| Bytes | Lang | Time | Link |
|---|---|---|---|
| 126 | Microsoft Excel | 241031T170030Z | General |
| 063 | Arturo | 240919T091022Z | chunes |
| 073 | Clojure | 240918T223743Z | Martin P |
| 048 | C clang | 240820T142313Z | jdt |
| 053 | R | 240820T164247Z | Dominic |
| 046 | R | 240820T125629Z | Kirill L |
| 078 | tinylisp 2 | 240406T043201Z | DLosc |
| 020 | Haskell + hgl | 240404T183008Z | Wheat Wi |
| 055 | R | 240405T131932Z | Giuseppe |
| 044 | Ruby | 240405T073930Z | G B |
| 036 | Wolfram Language Mathematica | 240404T041246Z | att |
| 040 | Haskell | 240404T062929Z | totallyh |
| 065 | PowerShell Core | 240404T032434Z | Julian |
| 016 | Pip x | 240404T183836Z | DLosc |
| 069 | R | 240404T070154Z | pajonk |
| 008 | 05AB1E | 240404T065237Z | Kevin Cr |
| 7625 | Vyxal Wr | 240403T225235Z | lyxal |
| 055 | Python 3.8 | 240403T210710Z | Jonathan |
| 046 | JavaScript Node.js | 240403T203633Z | l4m2 |
Microsoft Excel, 126
LAMBDA(f,l,LET(i,INDEX(l,1),VSTACK(i,SCAN(i,SEQUENCE(COUNT(l)-1),LAMBDA(s,j,INDEX(f,MOD(j-1,COUNTA(f))+1)(s,INDEX(l,j+1)))))))
Ungolfed with usage example:
=LET(
tieScan,LAMBDA(f,l,
LET(
i,INDEX(l,1),
VSTACK(i,
SCAN(i,SEQUENCE(COUNT(l)-1),LAMBDA(s,j,
INDEX(f,MOD(j-1,COUNTA(f))+1)(s,INDEX(l,j+1))
))
)
)
),
tieScan(HSTACK(LAMBDA(a,b,a*b),LAMBDA(a,b,a+b)),{9;2;8;6;3})
)
Hooray for built-in SCAN.
Arturo, 68 63 bytes
$[a,b][i:0map b'c[h:<=?i=0->c->call a\[%-i,1size a]@[h,c]'i+1]]
If you told me this problem was specifically designed to stymie Arturo, I would believe you.
Clojure, 73 bytes
#(let[i(atom -1)](reductions(fn[a v]((nth(cycle %)(swap! i inc))a v))%2))
Example:
(#(let[i(atom -1)](reductions(fn[a v]((nth(cycle %)(swap! i inc))a v))%2))
[+, *, -, /]
[1, 2, 3, 4, 5, 6, 7, 4, 9, 10])
=> (1 3 9 5 1 7 49 45 5 15)
R, 53 bytes
\(L,v,M=0)Reduce(\(...)el(M<<-c(M,L)[-1])(...),v,,,T)
An approach in R based on actually implementing the Jelly tie function:
tie={M=0;\(L)el(M<<-c(M[-1],L))}
Calling tie(L) returns a function that cycles through L each time it's called.
So we can use this as an argument to R's standard scan-like function, Reduce(...,accumulate=TRUE) , similarly to Giuseppe's answer:
f=\(v,L)Reduce(\(...)tie(L)(...),v,,,T)
Incorporating the code for tie directly into the Reduce call golfs this down to 53 bytes: this is sadly still longer than Kirill L's answer using a different approach, but pleasingly competitive with those of Giuseppe & pajonk.
R, 46 bytes
\(L,v)c(a<-v[1],Map(\(x,y)a<<-x(a,y),L,v[-1]))
Building up on pajonk's and Giuseppe's answers, I find it shorter to Map over the lists of functions and values, maintaining an additional accumulator, rather than to force the actual Reduce to do what we need.
Note that Map formats the output as a list. It's +3 bytes to change to mapply, which would preserve the more readable vector output:
Attempt This Online!
tinylisp 2, 78 bytes
(d G(\(A D N)(c A(? N(G((h D)A(h N))(,(t D)(] 1 D))(t N))(
(\(D N)(G(h N)D(t N
The first line defines a helper function G; the second line is an anonymous function that performs the task. Try It Online!
Explanation
First, the main function:
(\(D N)(G(h N)D(t N)))
(\ ) ; Lambda function
(D N) ; that takes two args, D (list of Dyads) and N (list of Numbers):
(G ) ; Call G with the following arguments:
(h N) ; Head of N
D ; D
(t N) ; Tail of N
The recursive helper function does the actual work:
(d G(\(A D N)(c A(? N ... ()))))
(d G ) ; Define G to be
(\ ) ; a lambda function
(A D N) ; that takes an accumulator A, plus D & N as before:
(c A ) ; Cons A to...
(? N ) ; If N is not empty,
... ; recursive call (see below)
() ; else, nil
(G((h D)A(h N))(,(t D)(] 1 D))(t N))
(G ) ; Call G recursively on these args:
((h D) ) ; New accumulator value: call first function in D
A ; on the old accumulator
(h N) ; and the first number in N
(, ) ; New list of dyads: concatenate
(t D) ; the tail of D
(] 1 D) ; with a list of the first 1 elements of D
(t N) ; New list of numbers: tail of N
tinylisp, 84 bytes
(d G(q((A D N P)(c A(i N(G((h P)A(h N))D(t N)(i(t P)(t P)D))(
(q((D N)(G(h N)D(t N)D
Works similarly, but we avoid expensive library functions like concat by using a fourth argument, P, a partial list of dyads. Whenever P becomes empty, we refill it from D.
Haskell + hgl, 20 bytes
cr<<<lz(m<U id)<<zcz
Explanation
zczzips the lists together looping the first one to the length of the second.lzcreates a left-hand scanm<U idapply the function to the both values.crget the value discarding the leftover function.
In regular haskell this would be
map snd ... foldl1 (fmap . uncurry id) ... zip . cycle
Where (...) is the blackbird combinator.
Reflection
I'm quite happy with this. It's nice to see zcz getting used, especially because that's an operation I ported over from Jelly in the first place.
m<U id seems like it might be too long, but it's a very niche operation.
- There's already an operation
f <% gforU f < g. Maybe there could be af %< gforf < U g. It would be helpful here, but this is a pretty synthetic operation. U idcould itself be potentially useful, even if a little niche.
That's all I have for local optimizations of this. However, the overall operation appears to be useful.
- The "tie" operation is not usable in Haskell as a pure function. But the scan and fold of it are. So the scan and fold of it could be added wholesale. The original challenge claims its useful, so I'll take their word for it.
- The tie operation could be implemented as acting within a state-monad. In this case I would need a
scanMoperation to call it.scanMcould be more generally useful anyway so it should be added.
R, 56 55 bytes
\(L,v)Reduce(\(x,y){L<<-c(L[-1],`?`<-el(L));x?y},v,,,T)
Test harness borrowed from pajonk's answer; -1 byte thanks to pajonk.
A rare instance of golfing with the <<- operator, which in this case causes L to be re-defined in the parent environment; this is a few bytes more succinct than using a for loop and manually cycling through both lists.
Wolfram Language (Mathematica), 39 36 bytes
-1 from totallyhuman
FoldList[h=#;Last[h=##2|#&@@h]@##&]&
Input [funcs][list].
Reusing f[funcs] resumes the cycle wherever the last call left off, as should be expected.
However, calling f anew invalidates all previous f[funcs]s. We could fix that by uncurrying for +3 bytes: Try it online!
h=#;Last[h=##2|#&@@h]@##& tie (each call: rotate left and call the last function)
FoldList[ ] scan
Haskell, 40 bytes
(f:g)#(l:m:n)=l:(g++[f])#(f l m:n)
_#l=l
More functional approach, 51 46 bytes
f#(h:t)=scanl(flip id)h$zipWith flip(cycle f)t
Even more functional approach, 46 bytes
((map snd.scanl1(fmap.uncurry id)).).zip.cycle
Cool fmap usage brought to you by Wheat Wizard.
PowerShell Core, 65 bytes
param($n,$o)$n|%{if($p){$p=&$o[$i++%$o.count]$p $_}else{$p=$_}$p}
Pip -x, 16 bytes
FibYPy?(a@Uvyi)i
Takes the list of functions and the list of integers as command-line arguments. Attempt This Online!
Explanation
FibYPy?(a@Uvyi)i
; y is ""; v is -1 (implicit)
; Eval both command-line args as Pip objects (-x flag)
Fi ; For each integer i
b ; in the second argument (list of ints):
y? ; Is y truthy?
; If so (all iterations after the first):
Uv ; Increment v
a@ ; Use it to index cyclically into first argument (list of funcs)
( yi) ; Call that function on the arguments y and i
; If not (first iteration):
i ; Use i unchanged
YP ; Print that value and yank it into y for the next iteration
R, 69 bytes
\(L,v,k=v[1])for(i in c(v[-1],1))k=L[[F<-F%%length(L)+1]](print(k),i)
05AB1E, 8 bytes
(could have been 7 bytes, but there is a bug in the cumulative reduce-by builtins)
Å»I¾è.V¼
05AB1E lacks functions, but it can evaluate strings to mimic the behavior of functions.
First input is the list of integers; second it the list of function-strings.
Try it online or verify all test cases.
Explanation:
Å» # Cumulative left-reduce the first (implicit) input-list of integers:
I # Push the second input-list of function-strings
¾è # Pop and leave the (0-based modular) `¾`'th function-string
# (`¾` is 0 by default)
.V # Evaluate it as 05AB1E code
¼ # Increase `¾` by 1
# (after which the resulting list is output implicitly)
Vyxal Wr, 61 bitsv2, 7.625 bytes
¨Sḣ(:n?†
Bitstring:
0011000000110111100101111100011001011110101000100101000001110
Expects the numbers, then the functions on the stack.
Explained
¨Sḣ(:n?†
¨S # Set the input source to the list of functions
ḣ # Push the head of the numbers list and then the rest
( # For each number in the rest:
: # Duplicate the top of the stack
n # Push the number
? # Get the next function (cyclical input)
† # Call the function. The r flag swaps the arguments before executing
# The W flag wraps the stack before printing
💎
Created with the help of Luminespire.
Python 3.8, 55 bytes
lambda o,x,*v:[x]+[x:=a(x,n)for a,n in zip(o*len(v),v)]
An unnamed function that accepts a list of functions and some values and returns the cumulative list.