g | x | w | all
Bytes Lang Time Link
018tinylisp 2230110T200952ZDLosc
033Microsoft Excel241029T201434ZGeneral
011Kamilalisp231001T173904ZKamila S
001Vyxal210621T065012Zlyxal
068><> Fish230603T032218Zchunes
078Go221005T152904Zbigyihsu
014Elm230110T015501Zalephalp
020Arturo230110T013143Zchunes
049Rust221006T112358Zmousetai
019Standard ML MLton210621T153742ZD. Ben K
037tinylisp210622T181909ZDLosc
00305AB1E210812T130031ZKevin Cr
003Juby210627T062448ZCyoce
017Clojure210624T133631ZKirill L
007BQN210621T070143ZRazetime
588Binary Lambda Calculus210624T061016Zuser1011
020Mlatu210624T031420Zbrightly
021Python2210623T222531ZTheEagle
021Factor210623T101627Zchunes
017Groovy210623T091423ZPeter De
009Julia 1.0210621T072313Zdingledo
022Java OpenJDK 8210621T072330Zhyperneu
043JavaScript210622T163707ZTheJim01
053Squire commit a380c1f210622T024844ZEasyasPi
009Pip210622T024415ZDLosc
052C gcc210621T173101Zpizzapan
011J210621T231146ZJonah
007Wolfram Language Mathematica210621T174124Zatt
014Scala210621T151536Zuser
004Japt210621T112812ZShaggy
020JavaScript210621T064554ZShaggy
032Red210621T084813ZGalen Iv
021R >= 4.1210621T081734ZNick Ken
003Jelly210621T081422ZUnrelate
013Raku210621T080627ZSean
027Perl 5210621T080525ZNahuel F
038R210621T075727ZdigEmAll
000APL Dyalog Unicode210621T070045ZAdá
019Proton210621T071302Zhyperneu
019Ruby 2.7210621T071123Zuser1007
023C# Visual C# Interactive Compiler210621T070910ZLiefdeWe
006Factor + combinators210621T070326Zwasif
055Lua210621T065641Zwasif
002Jelly210621T064951Zhyperneu
011Haskell210621T064852ZNoLonger
027Python 3210621T064046Zfireflam

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.

Try It Online!

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.

Kamilalisp, 11 bytes

:^⍏&[#0 ⊙⍧]

Attempt This Online!

Vyxal, 1 byte

S

Try it Online!

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@':$'~\

Try it

(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}

Attempt This Online!

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}

Attempt This Online!

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

Arturo, 20 bytes

$[n a][map a'f->f n]

Try it

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.

Playground

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!)

Try it online!

tinylisp, 37 bytes

(d C(q((F N)(i F(c((h F)N)(C(t F)N))(

Try it online!

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 %))

Try it online!

Takes input as varargs where the first argument is the integer, and the rest are functions.

BQN, 7 bytes

{𝕎𝕩}¨⟜<

Try it!

-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

Try it online!

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.

Factor, 21 bytes

[ [ call ] with map ]

Try it online!


Factor, 12 bytes

cleave>array

Try it online!

Builtin.

Groovy, 17 bytes

{n,L->L*.call(n)}

Try it online!

Julia 1.0, 9 bytes

a^b=a.|>b

Try it online!

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))

Try it online!

-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));}

Try it online!

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);}

Try it online!

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'

Try it online!

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

Try it online!

Built-in. Input [L[n]].


Wolfram Language (Mathematica), 12 bytes

n#@n&/@#&

Try it online!

Input [n][L].

Scala, 14 bytes

x=>_.map(_(x))

Try it online!

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

Try it

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:

enter image description here

Here's a TIO-compatible version:

Red, 41 bytes

func[L n][collect[foreach g L[keep g n]]]

Try it online!

R >= 4.1, 21 bytes

\(l,i)Map(\(x)x(i),l)

Try it online!

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

ṛĿⱮ

Try it online!

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.

Raku, 13 bytes

{@^f».($^n)}

Try it online!

Perl 5, 27 bytes

sub{$x=shift;map&$_($x),@_}

Try it online!

R, 38 bytes

function(i,m)sapply(m,do.call,list(i))

Try it online!

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).

Try it online!

Alternatively, we could define an "apply" operator that takes \$L\$ on the left and \$n\$ on the right:

APL (Dyalog Unicode), 5 bytes

{⍺⍺⍵}

⍺⍺ is the left operand and is the right argument.

Try it online!

Proton, 19 bytes

L=>x=>[g(x)for g:L]

Try it online!

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]}}

Try it online!

Doesnt work on TIO cause of Numbered arguments.

C# (Visual C# Interactive Compiler), 23 bytes

a=>b=>b.Select(x=>x(a))

C# was born for this

Try it online!

Factor + combinators, 6 bytes

cleave

Try it online!

Built-in.

Lua, 55 bytes

function g(L,n)for k,v in pairs(L)do print(v(n))end end

Try it online!

Jelly, 2 bytes

v€

Try it online!

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

³Ç$СḊ

Try it online!

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]

Try it online!

Anonymous function that takes the list of functions and a number as arguments.