With the formula below, I keep receiving the error "Wrong number of arguments to IF. Expected between 2 and 3 arguments, but received 5 arguments."
Where have I gone wrong? It works up until a certain point and then the code throw up the above error... Am i missing a comma or bracket somewhere?
Any help greatly appreciated :)
=arrayformula(If(G5 <> "",if(H5 = "Y",if(C6 = "Sub-Total", ttime($M$3,vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C7 = "Sub-Total",B6 <>"",B5 <>""),ttime($M$3,vlookup(B6,Lookup!$A:$I,3,0),vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C8 = "Sub-Total",B7 <> "", B6 <> "",B5 <>""),ttime($M$3,vlookup(B7,Lookup!$A:$I,3,0),vlookup(B6,Lookup!$A:$I,3,0),vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C9 = "Sub-Total",B8 <> "", B7 <> "",B6 <>"",B5 <> ""),ttime($M$3,vlookup(B8,Lookup!$A:$I,3,0),vlookup(B7,Lookup!$A:$I,3,0),vlookup(B6,Lookup!$A:$I,3,0)),vlookup(B5,Lookup!$A:$I,3,0)),if(AND(C10 = "Sub-Total",B9 <> "", B8 <> "",B7 <>"",B6 <> "",B5 <> ""),ttime($M$3,vlookup(B9,Lookup!$A:$I,3,0),vlookup(B8,Lookup!$A:$I,3,0),vlookup(B7,Lookup!$A:$I,3,0)),vlookup(B6,Lookup!$A:$I,3,0)),vlookup(B5,Lookup!$A:$I,3,0))))+IF(C6 = "sub-total","",if(B5 <> "", G6 * 3,""))+IF(C6 = "sub-total","",if(B5 <> "", "15","")),"Not Despatched"),""))
try:
=ARRAYFORMULA(
IF(G5<>"",
IF(H5="Y",
IF(C6="Sub-Total", TTIME($M$3, VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C7="Sub-Total", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C8="Sub-Total", B7<>"", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B7, Lookup!$A:$I, 3, 0),
VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C9="Sub-Total", B8<>"", B7<>"", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B8, Lookup!$A:$I, 3, 0),
VLOOKUP(B7, Lookup!$A:$I, 3, 0),
VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)),
IF(AND(C10="Sub-Total", B9<>"", B8<>"", B7<>"", B6<>"", B5<>""), TTIME($M$3, VLOOKUP(B9, Lookup!$A:$I, 3, 0),
VLOOKUP(B8, Lookup!$A:$I, 3, 0),
VLOOKUP(B7, Lookup!$A:$I, 3, 0),
VLOOKUP(B6, Lookup!$A:$I, 3, 0),
VLOOKUP(B5, Lookup!$A:$I, 3, 0)))))))+
IF(C6="sub-total",,
IF(B5<>"", G6*3, ))+
IF(C6="sub-total",,
IF(B5<>"", "15", )), "Not Despatched"), ))
Related
I have the following code:
llvmbuilder->SetCurrentDebugLocation(llvm::DILocation::get(*llvmctx, 0, mypos, llvmsub));
Where llvmsub is constructed at each function definition beginning like so:
llvmsub = llvm::DISubprogram::getDistinct(*llvmctx, llvmcu->getFile(), currfunc->identifier, currfunc->linkage, llvmcu->getFile(), 0, nullptr, 0, nullptr, 0, 0, llvm::DINode::DIFlags::FlagZero, llvm::DISubprogram::toSPFlags(false, true, false), llvmcu);
Where currfunc->identifier, currfunc->linkage are strings - for my C function and llvmcu is defined as follows, in the beginning of my Translation Unit:
llvmcu = llvmdibuilder->createCompileUnit(llvm::dwarf::DW_LANG_C, llvmdibuilder->createFile(std::string{modulename}, ""), "regularc", false, "", 0);
I also set version like so:
mainmodule->addModuleFlag(llvm::Module::Warning, "Debug Info Version",
llvm::DEBUG_METADATA_VERSION);
The issue is that after generation (which looks like this):
0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "regularc", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
!1 = !DIFile(filename: "combined.pp_1", directory: "")
!2 = !{i32 2, !"Debug Info Version", i32 3}
!3 = !DILocation(line: 0, column: 49179, scope: !4)
!4 = distinct !DISubprogram(name: "checkkeyb", scope: !1, file: !1, spFlags: DISPFlagDefinition, unit: !0)
!5 = !DILocation(line: 0, column: 49215, scope: !4)
!6 = !DILocation(line: 0, column: 49227, scope: !4)
!7 = !DILocation(line: 0, column: 49241, scope: !4)
!8 = !DILocation(line: 0, column: 49255, scope: !4)
!9 = !DILocation(line: 0, column: 49288, scope: !4)
(The columns are so long because I actually store file offsets)
After I compiler with llc and then with clang -g I get no debug line information in lldb:
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x30)
* frame #0: 0x0000000100126cb0 libSDL-1.2.0.dylib`SDL_SetColorKey + 36
frame #1: 0x000000010000b444 a.out`vgainit + 76
frame #2: 0x000000010000f354 a.out`maininit + 28
frame #3: 0x000000010000fd48 a.out`main + 20
frame #4: 0x00000001ac887e50 dyld`start + 2544
In Raku, how does one write the equivalent of Haskell's span function?
In Haskell, given a predicate and a list, one can split the list into two parts:
the longest prefix of elements satisfying the predicate
the remainder of the list
For example, the Haskell expression …
span (< 10) [2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4]
… evaluates to …
([2,2,2,5,5,7],[13,9,6,2,20,4])
How does one write the Raku equivalent of Haskell's span function?
Update 1
Based on the answer of #chenyf, I developed the following span subroutine (additional later update reflects negated predicate within span required to remain faithful to the positive logic of Haskell's span function) …
sub span( &predicate, #numberList )
{
my &negatedPredicate = { ! &predicate($^x) } ;
my $idx = #numberList.first(&negatedPredicate):k ;
my #lst is Array[List] = #numberList[0..$idx-1], #numberList[$idx..*] ;
#lst ;
} # end sub span
sub MAIN()
{
my &myPredicate = { $_ <= 10 } ;
my #myNumberList is Array[Int] = [2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4] ;
my #result is Array[List] = span( &myPredicate, #myNumberList ) ;
say '#result is ...' ;
say #result ;
say '#result[0] is ...' ;
say #result[0] ;
say #result[0].WHAT ;
say '#result[1] is ...' ;
say #result[1] ;
say #result[1].WHAT ;
} # end sub MAIN
Program output is …
#result is ...
[(2 2 2 5 5 7) (13 9 6 2 20 4)]
#result[0] is ...
(2 2 2 5 5 7)
(List)
#result[1] is ...
(13 9 6 2 20 4)
(List)
Update 2
Utilizing information posted to StackOverflow concerning Raku's Nil, the following updated draft of subroutine span is …
sub span( &predicate, #numberList )
{
my &negatedPredicate = { ! &predicate($^x) } ;
my $idx = #numberList.first( &negatedPredicate ):k ;
if Nil ~~ any($idx) { $idx = #numberList.elems ; }
my List $returnList = (#numberList[0..$idx-1], #numberList[$idx..*]) ;
$returnList ;
} # end sub span
sub MAIN()
{
say span( { $_ == 0 }, [2, 2, 5, 7, 4, 0] ) ; # (() (2 2 5 7 4 0))
say span( { $_ < 6 }, (2, 2, 5, 7, 4, 0) ) ; # ((2 2 5) (7 4 0))
say span( { $_ != 9 }, [2, 2, 5, 7, 4, 0] ) ; # ((2 2 5 7 4 0) ())
} # end sub MAIN
I use first method and :k adverb, like this:
my #num = [2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4];
my $idx = #num.first(* > 10):k;
#num[0..$idx-1], #num[$idx..*];
A completely naive take on this:
sub split_on(#arr, &pred) {
my #arr1;
my #arr2 = #arr;
loop {
if not &pred(#arr2.first) {
last;
}
push #arr1: #arr2.shift
}
(#arr1, #arr2);
}
Create a new #arr1 and copy the array into #arr2. Loop, and if the predicate is not met for the first element in the array, it's the last time through. Otherwise, shift the first element off from #arr2 and push it onto #arr1.
When testing this:
my #a = [2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4];
my #b = split_on #a, -> $x { $x < 10 };
say #b;
The output is:
[[2 2 2 5 5 7] [13 9 6 2 20 4]]
Only problem here is... what if the predicate isn't met? Well, let's check if the list is empty or the predicate isn't met to terminate the loop.
sub split_on(#arr, &pred) {
my #arr1;
my #arr2 = #arr;
loop {
if !#arr2 || not &pred(#arr2.first) {
last;
}
push #arr1: #arr2.shift;
}
(#arr1, #arr2);
}
So I figured I'd throw my version in because I thought that classify could be helpful :
sub span( &predicate, #list ) {
#list
.classify({
state $f = True;
$f &&= &predicate($_);
$f.Int;
}){1,0}
.map( {$_ // []} )
}
The map at the end is to handle the situation where either the predicate is never or always true.
In his presentation 105 C++ Algorithms in 1 line* of Raku (*each) Daniel Sockwell discusses a function that almost answers your question. I've refactored it a bit to fit your question, but the changes are minor.
#| Return the index at which the list splits given a predicate.
sub partition-point(&p, #xs) {
my \zt = #xs.&{ $_ Z .skip };
my \mm = zt.map({ &p(.[0]) and !&p(.[1]) });
my \nn = mm <<&&>> #xs.keys;
return nn.first(?*)
}
#| Given a predicate p and a list xs, returns a tuple where first element is
#| longest prefix (possibly empty) of xs of elements that satisfy p and second
#| element is the remainder of the list.
sub span(&p, #xs) {
my \idx = partition-point &p, #xs;
idx.defined ?? (#xs[0..idx], #xs[idx^..*]) !! ([], #xs)
}
my #a = 2, 2, 2, 5, 5, 7, 13, 9, 6, 2, 20, 4;
say span { $_ < 10 }, #a; #=> ((2 2 2 5 5 7) (13 9 6 2 20 4))
say span { $_ < 5 }, [6, 7, 8, 1, 2, 3]; #=> ([] [6 7 8 1 2 3])
Version 6.e of raku will sport the new 'snip' function:
use v6.e;
dd (^10).snip( * < 5 );
#«((0, 1, 2, 3, 4), (5, 6, 7, 8, 9)).Seq»
OK the solution should be simple... like =IF(AG3 = "", "")
but I am unable to add the clause to my current formula as seen below: Any suggestions?
=IF(
IF(AF3 <> "y",
SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0))
,"0")
= "0", "", SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0)))
Let's say your current formula is "FORMULA", you would have to do the following:
=IF(AG3="",,FORMULA)
Now replace FORMULA with your actual formula and you get
=IF(AG3="",,IF(
IF(AF3 <> "y",
SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0))
,"0")
= "0", "", SUM(IFNA(VLOOKUP($AG3, RICS_TimeClocks!Q$3:U, 4, 0), 0),
IFNA(VLOOKUP($AG3, RICS_TimeClocks!V$3:Z, 4, 0), 0))))
Suppose that I have a macro:
#define FOO(a, ...) if (a) foo(a, ## __VA_ARGS__)
This works well:
FOO(a) will be transformed to if (a) foo(a)
FOO(a, <some_parameters>) will be transformed to if (a) foo(a, <some_parameters>)
Is it possible to modify this macro, so only the first parameter of __VA_ARGS__ (if exists) passed to foo? So, I need:
FOO(a) to be transformed to if (a) foo(a)
FOO(a, b, <some_parameters>) to be transformed to if (a) foo(a, b)
I've tried to solve this with the same idea as BOOST_PP_VARIADIC_SIZE has, but it turned out this macro returns 1 for BOOST_PP_VARIADIC_SIZE() (empty arguments), which is not expected (I expected 0).
Note, that I need a solution, where b and <some_parameters> are evaluated only when bool(a) is true.
I propose a variadic macro with a generic lambda as a solution.
The Important points are as follows:
It is difficult to pass both a and __VA_ARGS__ to a lambda as passed arguments in macro because when __VA_ARGS__ is empty
[](){...}(a, __VA_ARGS__)
becomes
[](){...}(a,)
and this , leads compilation error.
Thus we split the first and second arguments of FOO into the captured and the passed ones respectively as follows.
Then we can use a generic lambda in the macro even if __VA_ARGS__ is empty.
[a](){...}(__VA_ARGS__)
The size of __VA_ARGS__ can be evaluated at compile-time as constexpr auto N. Then we can use if constexpr to separate function calls.
We can also apply if statement with initializer which is introduced from C++17 for if(a).
Then the proposed macro is as follows.
This also works for you.
DEMO
#include <tuple>
#define FOO(a, ...) \
if(const bool a_ = (a); a_) \
[a_](auto&&... args) \
{ \
const auto t = std::make_tuple(std::forward<decltype(args)>(args)...); \
constexpr auto N = std::tuple_size<decltype(t)>::value; \
\
if constexpr( N==0 ) { \
return foo(a_); \
} \
else { \
return foo(a_, std::get<0>(t)); \
} \
}(__VA_ARGS__)
Based on this answer, I could solve the problem:
#define PRIVATE_CONCAT(a, b) a ## b
#define CONCAT(a, b) PRIVATE_CONCAT(a, b)
#define GET_100TH( \
_01, _02, _03, _04, _05, _06, _07, _08, _09, _10, \
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
_21, _22, _23, _24, _25, _26, _27, _28, _29, _30, \
_31, _32, _33, _34, _35, _36, _37, _38, _39, _40, \
_41, _42, _43, _44, _45, _46, _47, _48, _49, _50, \
_51, _52, _53, _54, _55, _56, _57, _58, _59, _60, \
_61, _62, _63, _64, _65, _66, _67, _68, _69, _70, \
_71, _72, _73, _74, _75, _76, _77, _78, _79, _80, \
_81, _82, _83, _84, _85, _86, _87, _88, _89, _90, \
_91, _92, _93, _94, _95, _96, _97, _98, _99, PAR, \
...) PAR
#define HAS_PARAMETER(...) GET_100TH(placeholder, ##__VA_ARGS__, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 0)
#define FIRST_PARAMETER_WITH_PREPENDED_COMMA0(...)
#define FIRST_PARAMETER_WITH_PREPENDED_COMMA1(a, ...) , a
#define FIRST_PARAMETER_WITH_PREPENDED_COMMA(...) CONCAT(FIRST_PARAMETER_WITH_PREPENDED_COMMA, HAS_PARAMETER(__VA_ARGS__))(__VA_ARGS__)
#define FOO(a, ...) if (a) foo(a FIRST_PARAMETER_WITH_PREPENDED_COMMA(__VA_ARGS__))
I have a function that cycles through two sperate lists and combines them into one as follows:
spread = Table[{gld[[i, 1]], (gld[[i, 2]] - gdx[[i, 2]]) },
{i, 1, Length[gld], 1}]
This works fine, and generates answers in the form:
{{2009, 6, 1}, 52.72}
But when I add a subtraction, as follows:
spread = Table[{gld[[i, 1]], (gld[[i, 2]] - gdx[[i, 2]]) - meanspread },
{i, 1, Length[gld], 1}]
I get answers in the format:
{{2009, 6, 1}, {-20.2896}}
This causes issues when I want to use DateLinePlot (all the data is in the extreme right of the graph, and the graph is not usable.
Can anyone suggest what might be happening here and how I may avoid it?
Thanks!
Most likely meanspread is not a number, but a single-item list, such as {1.1}. It's impossible to tell without knowing more details and having a sample of all data/variables you're using.
I don't get this, recreating your inputs as best I can. It really depends on how you're computing meanspread.
(*In[2]:= *)
gld = FinancialData["NYSE:GLD", "Close", {"June 1, 2009", DateString[], "Day"}];
gdx = FinancialData["NYSE:GDX", "Close", {"June 1, 2009", DateString[], "Day"}];
(*In[5]:= *)
First[spread = Table[{gld[[i, 1]], (gld[[i, 2]] - gdx[[i, 2]])}, {i, 1, Length[gld], 1}]]
(*Out[5]= *)
{{2009, 6, 1}, 52.72}
(*In[8]:= *)
meanspread = Mean[spread[[All, 2]]]
(*Out[8]= *)
74.0373
(*In[9]:= *)
First[Table[{gld[[i, 1]], (gld[[i, 2]] - gdx[[i, 2]]) - meanspread}, {i, 1, Length[gld], 1}]]
(*Out[9]= *)
{{2009, 6, 1}, -21.3173}
I think you would benefit from a simpler construction.
spread = {gld[[All, 1]], gld[[All, 2]] - gdx[[All, 2]] - meanspread}\[Transpose]
As already said, if meanspread is a single numerical value, and not a list, the output should be correct.