| Bytes | Lang | Time | Link |
|---|---|---|---|
| 018 | tinylisp 2 | 230110T200952Z | DLosc |
| 033 | Microsoft Excel | 241029T201434Z | General |
| 011 | Kamilalisp | 231001T173904Z | Kamila S |
| 001 | Vyxal | 210621T065012Z | lyxal |
| 068 | ><> Fish | 230603T032218Z | chunes |
| 078 | Go | 221005T152904Z | bigyihsu |
| 014 | Elm | 230110T015501Z | alephalp |
| 020 | Arturo | 230110T013143Z | chunes |
| 049 | Rust | 221006T112358Z | mousetai |
| 019 | Standard ML MLton | 210621T153742Z | D. Ben K |
| 037 | tinylisp | 210622T181909Z | DLosc |
| 003 | 05AB1E | 210812T130031Z | Kevin Cr |
| 003 | Juby | 210627T062448Z | Cyoce |
| 017 | Clojure | 210624T133631Z | Kirill L |
| 007 | BQN | 210621T070143Z | Razetime |
| 588 | Binary Lambda Calculus | 210624T061016Z | user1011 |
| 020 | Mlatu | 210624T031420Z | brightly |
| 021 | Python2 | 210623T222531Z | TheEagle |
| 021 | Factor | 210623T101627Z | chunes |
| 017 | Groovy | 210623T091423Z | Peter De |
| 009 | Julia 1.0 | 210621T072313Z | dingledo |
| 022 | Java OpenJDK 8 | 210621T072330Z | hyperneu |
| 043 | JavaScript | 210622T163707Z | TheJim01 |
| 053 | Squire commit a380c1f | 210622T024844Z | EasyasPi |
| 009 | Pip | 210622T024415Z | DLosc |
| 052 | C gcc | 210621T173101Z | pizzapan |
| 011 | J | 210621T231146Z | Jonah |
| 007 | Wolfram Language Mathematica | 210621T174124Z | att |
| 014 | Scala | 210621T151536Z | user |
| 004 | Japt | 210621T112812Z | Shaggy |
| 020 | JavaScript | 210621T064554Z | Shaggy |
| 032 | Red | 210621T084813Z | Galen Iv |
| 021 | R >= 4.1 | 210621T081734Z | Nick Ken |
| 003 | Jelly | 210621T081422Z | Unrelate |
| 013 | Raku | 210621T080627Z | Sean |
| 027 | Perl 5 | 210621T080525Z | Nahuel F |
| 038 | R | 210621T075727Z | digEmAll |
| 000 | APL Dyalog Unicode | 210621T070045Z | Adá |
| 019 | Proton | 210621T071302Z | hyperneu |
| 019 | Ruby 2.7 | 210621T071123Z | user1007 |
| 023 | C# Visual C# Interactive Compiler | 210621T070910Z | LiefdeWe |
| 006 | Factor + combinators | 210621T070326Z | wasif |
| 055 | Lua | 210621T065641Z | wasif |
| 002 | Jelly | 210621T064951Z | hyperneu |
| 011 | Haskell | 210621T064852Z | NoLonger |
| 027 | Python 3 | 210621T064046Z | fireflam |
tinylisp 2, 21 18 bytes
(\(N)(p m(\(F)(F N
An anonymous function that takes its arguments curried: calling it on an integer returns a second function, and then calling that on a list of functions performs the cleaving.
Explanation
The lexical scoping of tinylisp 2 makes this solution much simpler than my tinylisp solution.
(\(N)(p m(\(F)(F N))))
(\ ) ; Lambda function
(N) ; that takes an integer N:
(p m ) ; Partially apply the map function to
(\ ) ; another lambda function
(F) ; that takes a function F:
(F N) ; Call F on N
The return value is a function that takes a list and maps the inner lambda over it. Calling this on a list of functions gives the desired result.
Microsoft Excel, 33
LAMBDA(i,p,MAP(p,LAMBDA(q,q(i))))
Usage example
=LET(
cleave,LAMBDA(i,p,MAP(p,LAMBDA(q,q(i)))),
cleave(1,VSTACK(LAMBDA(x,x+1),LAMBDA(x,x+2)))
)
Gives {2;3}. VSTACK is the only way to get an array of LAMBDAs. Excel does not let you put anything but literals in arrays.
Note for porting: Google Sheets does not acknowledge lambdas in arrays. It lets you create them just fine, but it won't let you call them. Hopefully one day they will fix this.
Vyxal, 1 byte
S
A full program that takes number then functions.
Explained
S # Converts each function to a string. This involves evaluating them, which somehow takes input from the stack.
Takes input in the header because that's how to input functions
><> (Fish), 68 bytes
r1l2(?\$:' '=?/$:@2p1+10.
/$i /
\
\$~{~r
>l ?\;/0.
\oan/ \e@':$'~\
(Assuming leading space is acceptable in input. See this for 76 bytes if not.)
Well this is challenging. My strategy for accepting a list of functions is to parse them from input separated by spaces, then write them to the source code along with some piping to preserve the input number. Then I push the input number to the stack and send the instruction pointer to the third row which is where the functions are written. Then I clean up some garbage values and output the result.
Go, 78 bytes
func(x int,F[]func(int)int)(O[]int){for _,f:=range F{O=append(O,f(x))}
return}
Go, 79 bytes, generic
func f[T any](x T,F[]func(T)T)(O[]T){for _,f:=range F{O=append(O,f(x))}
return}
Elm, 14 bytes
(|>)>>List.map
|> is function application: a |> f means f a.
>> is function composition: (f >> g) a is g (f a)).
You can try it here. Here is a full test program:
import Html exposing (text)
f = (|>)>>List.map
main =
f 5 [\x -> x * 2, \x -> x - 1, \x -> x * x]
|> Debug.toString
|> text
Rust, 49 bytes
|u,k:Vec<Box<_>>|k.iter().map(|z|z(u)).collect();
Maximally abusing this ruling. Would be nearly twice as long if all types where specified.
Standard ML (MLton), 19 bytes
(-1 from Laikoni by manipulating names and spacing on the original fn n=>map(fn f=>f x).)
fn! =>map(fn f=>f!)
tinylisp, 37 bytes
(d C(q((F N)(i F(c((h F)N)(C(t F)N))(
Ungolfed/explanation
(load library) Library contains ungolfed aliases for builtins
(def cleave Define cleave to be
(lambda (F N) a function taking a list of functions F and a number N
(if F If F is nonempty:
(cons Construct a list
((head F) N) whose head is the first function in F applied to N
(cleave and whose tail is the result of a recursive call
(tail F) with the remaining functions in F
N)) and the same number N
nil)))) Else, return empty list
05AB1E, 3 bytes
€.V
Try it online or verify all test cases.
Explanation:
€ # Map over each function-string in the first (implicit) input-list:
.V # Evaluate it as 05AB1E code, with the second (implicit) input-integer as argument
# (and output the resulting list of values implicitly)
J-uby, 3 Bytes
:-@
J-uby actually has a built-in for this, despite me never having actually used it. You are intended to use it with -[f, g, h] to create a function that will "cleave" a provided argument over the functions in the array. :-@ is the symbol for unary minus in Ruby, which is callable in J-uby. It would take input in the form :-@.call(L).call(n).
But built-ins are boring. How would it be implemented otherwise?
13 Bytes
~:*%(:& &~:^)
There are basically three components to this function. First is ~:* which just creates a flipped-argument map function: ->(a, f){ a.map(&f) }.
Next, there is :& &~:^. This defines the "n-applicator" function: given the input n, this defines a function that will apply n to whatever function you pass it. Essentially, it’s equivalent to ->(n){ ->(f){ f[n] } }. How does this work? ^ is the function application operator, so ~:^ is function application with flipped arguments: ->(x, f){ f[x] }. Since & is the partial application operator, :& & ~:^ means you partially apply the flipped function application operator to the partial application operator. Anyone else getting semantic satiation?
Finally, we've got % in the middle. The % operator has different functionalities depending on its arguments, but in this case it denotes a "hook": given a binary function F and a unary function G, it returns a function that applies G to one of its two arguments before passing them on to F: F % G == ->(L,n){ F[L, G[n]] }. In this case, that means calling a flipped map (~:*) on the input array L and the n-applicator function (:& & ~:^).
The result is mapping the n-applicator function over the supplied array of functions, applying n to each element of the array. In plain Ruby, it's equivalent to ->(L,n){ L.map { |f| f[n] } }
Clojure, 17 bytes
#(for[f %&](f %))
Takes input as varargs where the first argument is the integer, and the rest are functions.
BQN, 7 bytes
{𝕎𝕩}¨⟜<
-2 bytes from dzaima.
Takes a list of monadic BQN functions, returns a list of the same length.
Binary Lambda Calculus, 58 bits = 8 bytes
0000010110000000000101110011111011111100101111011010000010
It's a higher-order function that takes in a Church numeral and a list and returns a list. The list encoding is the one where a list is identified with its right fold function (e.g. [1, 2] would be \c n. c 1 (c 2 n)). (I'm using \ as \$\lambda\$ in this answer.)
“Readable” version:
\\@@1\\\\@@2@46@@321\\1
Here, I'm using prefix notation. \ is abstraction and @ is application. Numbers are de Bruijin indices (starting at 1). Of course, (for example) 321 here means 3, then 2, then 1, not the number 321.
Actually readable version:
\k l. l (\a b c n. c (a k) (b c n)) (\c n. n)
Explanation
Todo. It’s late and I’m tired.
Mlatu, 20 bytes
->n;{n swap call}map
Explanation:
->n; pops off the stack and binds it to n. The list is left as the implicit parameter of map and is never bound. {n swap call} is a closure in Mlatu, which pushes n (the number) onto the stack, swaps the placement of the number and the function, and then calls the function with n as its argument. map takes the list and the closure and maps the list with the given closure.
Python2, 21 bytes
print[f(n)for f in l]
Makes use of Python 2's print being a keyword.
Julia 1.0, 9 bytes
a^b=a.|>b
Julia 1.6+, 3 bytes
As pointed out by @MarcMush, Julia 1.6 has since allowed defining .|> as a function, giving this 3-byter:
.|>
TIO does not offer Julia 1.6, but here's an interactive example:
julia> f=.|>
Base.Broadcast.BroadcastFunction(|>)
julia> println(f(5, [x->x*2, x->x-1, x->x*x]))
[10, 4, 25]
Java (OpenJDK 8), 22 bytes
L->x->L.map(a->a.f(x))
-4 bytes thanks to Olivier Grégoire
L is a stream of Function<Integer, Integer> and x is an int. Function here is not java.util.function.Function but rather is a custom interface Function<T, U> with a single method U f(T t). This is just to save bytes over the normal Function<T, U> which requires the method name apply, which is apparently allowed.
JavaScript, 43 bytes
I know there's already a nice JS entry, but I wanted to give it a go by giving both parameters to a single function. Different approach, same output.
Here's mine:
(n,a)=>a.reduce((A,F)=>A.concat([F(n)]),[])
Try it online! Also test cases (modified from Arnauld's imlementation).
This is NOT memory friendly because I had to create a new array each time I used Array.prototype.concat. It would have been much nicer and slightly shorter if JavaScript's Array.prototype.push would just return the modified array...
Squire (commit a380c1f), 53 bytes
journey f(n,a){i=N whilst(t=a[i=i+I])!=ni{a[i]=t(n)}}
Thou shalt call f like so:
# Example journeys
journey mul(n)
{
reward n * II
}
journey sub(n)
{
reward n - I
}
journey squ(n)
{
reward n * n
}
# Createth a quest of journeys
quest = [ mul, sub, squ ]
# Number to cleave (thou might also use V directly)
n = V
# Embark on journey f
# f modifieth array in place.
f(n, array)
# Useth how thy wishes
# Array(Number(10), Number(4), Number(25))
dump(array)
Ungolfeth:
# Createth a journey named "cleave"
# It accepteth 2 arguments.
journey cleave(n, array)
{
# Defineth a counter variable i with the mere value N.
# Arrays are I-indexed; but we want to increment as we do things.
counter = N
# Alas, Squire doth not yet supporteth embarking directly
# on journeys in arrays, so we musteth hire a mercenary.
# Useth a property that going outside of an array's
# kingdom will reward naught but ni ("null").
whilst (mercenary = array[i = i + I]) != ni
{
# Embark the mercenary on his journey and
# overwriteth array[i] with its reward.
array[i] = mercenary(n)
}
}
Note: I am trying to find a way to embed Squire on TIO, without much luck. It comes too close to the character limit to be useful.
Pip, 9 bytes
@Z{bM[a]}
A function that takes a number and a list of functions and returns a list of results. Try it online!
Explanation
The Map operator has a built-in ability to take a list of functions as its left argument. Unfortunately, it still expects the right argument to be a list of values, so we have to do some boxing and unboxing:
{ } Anonymous function
bM Map (each function in) the second argument
[a] to a list containing the first argument
The result is a list of single-element lists
Z Zip: now we have a list containing a single list
@ Get the first (and only) element
C (gcc), 62 52 bytes
f(n,L)int(**L)();{while(*L)printf("%d ",(*L++)(n));}
Takes L as a NULL-terminated array of pointers to functions taking an int and returning an int. Outputs results space-separated on stdout (with a trailing space).
-10 bytes from @EasyasPi
(also C++ (gcc), 79 bytes with #include<cstdio> and ANSI function syntax. Try it online! (C++))
If writing to a caller-allocated array that is passed to the function is a valid form of output, then:
C (gcc), 59 51 bytes
f(int n,int(**L)(),int*r){while(*L)*r++=(*L++)(n);}
Same input as above, but outputs results through r, which must point to at least as many ints as L does int(*)(int)s (not including L's NULL terminator).
-8 bytes from @EasyasPi
(also C++ (gcc), 59 bytes [Try it online! (C++)][TIO-kq6vpd7q])
J, 11 bytes
4 :'x`:0 y'
Called like:
+:`<:`*: f 5
In J, you cannot pass around a list "functions" (aka "verbs") directly, but you can pass around a list of "gerunds", which, like English gerunds, are noun-ified verbs.
Then you can use the "Evoke Gerund" conjunction, specifically the `:0 form of it, to call those gerunds as verbs again.
Doing this requires (afaik) an explicit verb, because conjunctions can only apply to a literal or a named variable. Here, the default LHS variable x represents the list of gerunds we are "evoking", and the integer we are applying them to is y.
worth noting
This would be a very un-J thing to do, and if you wanted to solve this specific problem the natural solution would be to create a train like this:
+:,<:,*:
which creates a single new verb, rather than a list of verbs, but will produce the same output as the submission above when applied to 5.
Wolfram Language (Mathematica), 7 bytes
Through
Built-in. Input [L[n]].
Wolfram Language (Mathematica), 12 bytes
n#@n&/@#&
Input [n][L].
Japt, 4 bytes
Takes the integer as input as a single digit array with the array of functions pre-assigned to variable V, outputs and array of single integer arrays.
V£mX
JavaScript, 20 bytes
a=>n=>a.map(f=>f(n))
Try it online! or run all test cases (courtesy of Arnauld)
Red, 32 bytes
func[g n][forall g[g/1: g/1 n]g]
Modifies the list in place.
Doesn't work in TIO, but works fine in the Red GUI console:
Here's a TIO-compatible version:
Red, 41 bytes
func[L n][collect[foreach g L[keep g n]]]
R >= 4.1, 21 bytes
\(l,i)Map(\(x)x(i),l)
An anonymous function taking a list of functions and an integer and returning a list of integers.
TIO version uses function instead of \ since TIO hasn’t been upgraded to R 4.1 yet.
Jelly, 3 bytes
ṛĿⱮ
Takes \$n\$ on the left and a list of link indices on the right.
Ɱ For each element of the right argument,
ṛĿ call the link at that index monadically on the left argument.
APL (Dyalog Unicode), 0 bytes
APL doesn't normally use lists of functions, but there are ways to use such. Even if the syntax for their creation is awkward, their use is simple: Try it online!
For the specific case of a list of integer↦integer functions, it is convenient to represent \$L\$ as a train, though it, despite appearances, isn't a list of functions. The calling syntax is identical to the above: Try it online!
If we absolutely wanted something that took \$L\$ and \$n\$ and applied \$L\$ to \$n\$ using the above juxtaposition syntax, we write a full program which prompts first for \$\$ and then for \$L\$, applying \$L\$ to \$n\$ by juxtaposition of the input values:
APL (Dyalog Unicode), 2 bytes
⎕⎕
Each stylised console prompts for a value from the console (STDIN).
Alternatively, we could define an "apply" operator that takes \$L\$ on the left and \$n\$ on the right:
APL (Dyalog Unicode), 5 bytes
{⍺⍺⍵}
Proton, 19 bytes
L=>x=>[g(x)for g:L]
You might be wondering why I'm doing fs.append on each element. That's because making a list of functions errors when you try to print it or iterate through it - that's because it never becomes a list.
Observe this and notice that making a tuple of functions in Proton actually automatically cleaves.
Ruby 2.7, 19 bytes
->a,b{b.map{_1[a]}}
Doesnt work on TIO cause of Numbered arguments.
Jelly, 2 bytes
v€
Accepts a list of functions on the left in Jelly source code and the value on the right.
v€ Main Link; (x: functions, y: value)
€ For each function in x
v Evaluate it at y
Less trivial answer:
Jelly, 6 bytes
³Ç$СḊ
Full program that accepts the blackbox-functions as a tied function in the header (standard for inputting blackbox-functions for Jelly), the initial value in the third argument (first value), and the number of functions in the fourth argument (second value).
³Ç$СḊ Main Link; (x: value, y: number of functions)
С Collecting intermediate values, repeat y times:
$ - last two:
³ - x
Ç - call the helper link (the blackbox)
Ḋ All but the first element
Basically, calls the black-box N times, where each time the function cycles its behavior (that's how tie works), and collects all N+1 results, then removes the initial value.
Haskell, 11 bytes
map.flip($)
(If using Stack, this can be dropped to 7 bytes: map.(&).)
Python 3, 27 bytes
lambda L,n:[g(n)for g in L]
Anonymous function that takes the list of functions and a number as arguments.
