32
\$\begingroup\$

Write the shortest program that takes one input (n) from STDIN (or equivalent) and outputs a simple incrementing function with one argument (x) that returns x + n but the function must be in a different language. Pretty simple!

This is code-golf, normal rules apply, shortest program wins.

Example: ><> to Python (Ungolfed)

!v"def i(x):"a"    return x+"ir!
 >l?!;o

Input:

3

Output:

def i(x):
    return x+3

EDIT: Anonymous functions and lambda expressions are allowed!

\$\endgroup\$
13
  • 1
    \$\begingroup\$ How big can the input get? As far as I can tell, your example only works with single digit numbers for the ><> half of it. \$\endgroup\$
    – Sp3000
    Commented Nov 9, 2015 at 6:12
  • 1
    \$\begingroup\$ In theory in should be able to use any (reasonable) input, but answers that only use a input that could maintain 1 digit are fully acceptable, I did consider reworking the example to correct this before posting but i figured ill leave it for simplicity stake. \$\endgroup\$ Commented Nov 9, 2015 at 6:32
  • 9
    \$\begingroup\$ I don't see any definition for function BUT that we are supposed to write.. -_- \$\endgroup\$
    – Optimizer
    Commented Nov 9, 2015 at 13:45
  • 1
    \$\begingroup\$ When a function returns another function, it's called a closure. I don't know if this applies cross-language, though... \$\endgroup\$ Commented Nov 9, 2015 at 15:31
  • 1
    \$\begingroup\$ @ETHproductions I guess the goal is not to return a function object, but the source code of a function in that other language. \$\endgroup\$ Commented Nov 9, 2015 at 20:40

65 Answers 65

26
\$\begingroup\$

GS2 → K, 2 bytes

•+

This prints a tacit, monadic function. The source code uses the CP437 encoding. Try it online!

Test run

$ xxd -c 2 -g 1 sum-func.gs2
00000000: 07 2b  .+
$ printf 42 | gs2 sum-func.gs2
42+
$ kona
K Console - Enter \ for help

  (42+) 69
111
  f : 42+
42+
  f 69
111

How it works

GS2

  • GS2 automatically reads from STDIN and pushes the input on the stack.

  • indicates that the next byte is a singleton string literal.

  • Before exiting, GS2 prints all stack items.

K

Left argument currying is automatic in K.

Here, n+ turns the dyadic function + into a monadic function by setting its left argument to n.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Which encoding are you using? \$\endgroup\$ Commented Nov 9, 2015 at 15:30
  • 2
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ "The source code uses the CP437 encoding." \$\endgroup\$ Commented Nov 9, 2015 at 16:02
16
\$\begingroup\$

ShapeScript → J, 4 bytes

"&+"

This prints a tacit, monadic verb. Try it online: ShapeScript, J

Test run

$ cat sum-func.shape; echo
"&+"
$ printf 42 | shapescript sum-func.shape; echo
42&+
$ j64-804/jconsole.sh 
   42&+ 69
111
   f =: 42&+
   f 69
111

How it works

ShapeScript

  • ShapeScript automatically reads from STDIN and pushes the input on the stack.

  • "&+" pushes that string on the stack.

  • Before exiting, ShapeScript prints all stack items.

J

& performs argument currying.

Here, n&+ turns the dyadic verb + into a monadic verb by setting its left argument to n.

\$\endgroup\$
2
  • \$\begingroup\$ I'm pretty sure there's a language where you don't need the close-quote. \$\endgroup\$
    – lirtosiast
    Commented Nov 9, 2015 at 6:04
  • \$\begingroup\$ There's a good chance you're right, but I can't recall one with implicit input and implicit output. \$\endgroup\$
    – Dennis
    Commented Nov 9, 2015 at 6:06
12
\$\begingroup\$

GolfScript → CJam, 4 bytes

{+}+

This prints a code block (anonymous function). Try it online: GolfScript, CJam

Test run

$ cat sum-func.gs; echo
{+}+
$ printf 42 | golfscript sum-func.gs
{42 +}
$ cjam
> 69 {42 +} ~
111
> {42 +}:F; 69F    
111

How it works

GolfScript

  • GolfScript automatically reads from STDIN and pushes the input on the stack.

  • {+} pushes that block on the stack.

  • + performs concatenation, which happily concatenates a string and a block.

  • Before exiting, GolfScript prints all stack items.

CJam

{n +} is a code block that, when executed, first pushes n on the stack, then executes +, which pops two integers from the stack and pushes their sum.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ I was just going to post this! \$\endgroup\$
    – xenia
    Commented Nov 9, 2015 at 6:36
  • \$\begingroup\$ Wow that's impressive. {n +} pushes 42, then executes +. (Probably should be {42 +} or "pushes n") \$\endgroup\$
    – Justin
    Commented Nov 11, 2015 at 7:10
  • \$\begingroup\$ @Justin Indeed. Thanks! \$\endgroup\$
    – Dennis
    Commented Nov 11, 2015 at 14:20
11
\$\begingroup\$

BrainF*** to JavaScript ES6, 57 bytes

----[-->+++<]>--.[-->+<]>+.+.--[->++<]>.[--->+<]>+++.[,.]

(Assumes that the input is composed of numeric characters)

Say 1337 is your input. Then, this would compile to:

x=>x+1337
\$\endgroup\$
0
10
\$\begingroup\$

R to Julia, 19 bytes

cat("x->x+",scan())

This reads an integer from STDIN using scan() and writes an unnamed Julia function to STDOUT using cat(). The Julia function is simply x->x+n, where n comes from the R program.

\$\endgroup\$
10
\$\begingroup\$

Rotor to K, 2 bytes

'+

Might as well jump in on the K bandwagon.

\$\endgroup\$
9
\$\begingroup\$

Malbolge to JavaScript ES6, 71 bytes

('&%@9]!~}43Wyxwvutsr)Mon+HGi4~fBBdR->=_]:[875t4rT}0/Pf,d*((II%GEE!Y}Az

It's always fun to generate Malbolge code.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 41 bytes \$\endgroup\$
    – ceilingcat
    Commented Jun 19, 2020 at 4:50
  • \$\begingroup\$ @ceilingcat Not working; missing cat program part. \$\endgroup\$ Commented Nov 2, 2022 at 8:16
  • \$\begingroup\$ The original 71 bytes program is also missing cat part. \$\endgroup\$ Commented Nov 2, 2022 at 8:17
9
\$\begingroup\$

Minecraft 1.8.7 to K, 7 6 + 33 + 27 + 62 = 129 128 Bytes

This is using this version of byte counting.

the system

Command blocks (going from left to right):

scoreboard objectives add K dummy
scoreboard players set J K <input>
tellraw @a {score:{name:"J",objective:"K"},extra:[{text:"+"}]}

This could probably be golfed a little more, but it's fairly simple: generate a variable J with the objective K and set its score for that objective to the input (there is no STDIN - I figured this was close enough). Then, after a tick, output the score of the variable J for the objective K followed by a +. Easy peasy.

\$\endgroup\$
3
  • \$\begingroup\$ Note that the .dat bytecounting is not applicable for this system, as the Minecraft version listed does not support structure files. \$\endgroup\$ Commented Feb 21, 2017 at 9:02
  • \$\begingroup\$ For the second one, couldn't you remove the extra, place the score JSON in an array, and place a string after? tellraw @a [{score:{name:"J",objective:"K"}},"+"] \$\endgroup\$
    – rydwolf
    Commented Nov 9, 2019 at 16:04
  • \$\begingroup\$ @RedwolfPrograms Not at the time of submission, four years ago. \$\endgroup\$ Commented Nov 10, 2019 at 4:44
9
\$\begingroup\$

O to K, 5 bytes

i'++o

Thanks to @kirbyfan64sos

Another version using features added after the challenge was created.

i'+
  • Gets input, pushes to stack
  • Pushes '+' as a string
  • Outputs stack contents
\$\endgroup\$
5
  • \$\begingroup\$ K has automatic currying, so you can just do i'++p. \$\endgroup\$ Commented Nov 9, 2015 at 15:51
  • \$\begingroup\$ The distance between K and O is 4. You need to shorten it one byte. \$\endgroup\$
    – mbomb007
    Commented Nov 11, 2015 at 22:39
  • \$\begingroup\$ @mbomb007 wat. The code is 5 bytes long \$\endgroup\$
    – jado
    Commented Nov 11, 2015 at 22:42
  • 2
    \$\begingroup\$ The letters. In the alphabet. Get it? \$\endgroup\$
    – mbomb007
    Commented Nov 11, 2015 at 22:42
  • 2
    \$\begingroup\$ @mbomb007 that was too complicated for me \$\endgroup\$
    – jado
    Commented Nov 11, 2015 at 23:05
8
\$\begingroup\$

Seriously to Python, 15 bytes

,"lambda n:n+"+

Expects input to be in string form, i.e. "3"

Explanation:

,: read value from input
"lambda n:n+": push this literal string
+: concatenate top two values on stack

Try it online (you will have to manually enter the input because the permalinks don't like quotes)

\$\endgroup\$
3
  • \$\begingroup\$ Hey, someone actually went through with Seriously! :D \$\endgroup\$ Commented Nov 9, 2015 at 15:26
  • 2
    \$\begingroup\$ Seriously? You finished Seriously before Simplex? D: \$\endgroup\$ Commented Nov 9, 2015 at 15:28
  • 1
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ It's not fully finished yet (see the issue tracker), but it works well enough to use in some golfs. \$\endgroup\$
    – user45941
    Commented Nov 9, 2015 at 20:15
7
\$\begingroup\$

Pyth to APL, 7 5 bytes

+z"--

The Pyth code simply concatenates the input (z) with the string "--". This creates an unnamed monadic train in APL with the form n--, where n comes from Pyth. When calling it in APL, (n--)x for some argument x computes n--x = n-(-x) = n+x.

Try: Pyth, APL

Saved 2 bytes thanks to Dennis!

\$\endgroup\$
0
6
\$\begingroup\$

rs -> K, 2 bytes

/+

Live demo.

\$\endgroup\$
5
\$\begingroup\$

><> to Python, 25 + 3 = 28 bytes

"v+x:x adbmal
o/?(3l
;>~n

Takes input via the -v flag, e.g.

py -3 fish.py add.fish -v 27

and outputs a Python lambda, e.g. lambda x:x+27.

For a bonus, here's an STDIN input version for 30 bytes:

i:0(?v
x+"r~/"lambda x:
o;!?l<
\$\endgroup\$
0
5
\$\begingroup\$

Mouse to Ruby, 19 bytes

?N:"->x{x+"N.!"}"$

Ungolfed:

? N:       ~ Read an integer from STDIN, store in N
"->x{x+"   ~ Write that string to STOUT
N. !       ~ Write N
"}"$       ~ Close bracket, end of program

This creates an unnamed Ruby function of the form ->x{x+n} where n comes from Mouse.

\$\endgroup\$
1
  • \$\begingroup\$ Good job, I like this post. \$\endgroup\$
    – jado
    Commented Nov 11, 2015 at 23:58
5
\$\begingroup\$

Haskell to Mathematica, 14 bytes

(++"+#&").show
\$\endgroup\$
5
\$\begingroup\$

Mathematica to C#, 22 bytes

"x=>x+"<>InputString[]

Outputs a C# Func<int, int> of form

x=>x+n
\$\endgroup\$
1
  • 1
    \$\begingroup\$ The output is valid Javascript (ES6/7) as well. \$\endgroup\$ Commented Nov 10, 2015 at 22:27
4
\$\begingroup\$

Pyth -> K, 4 bytes

+z\+

K is really easy to abuse here...

Live demo.

\$\endgroup\$
4
\$\begingroup\$

Brainfuck to Java, 273

+[----->+++++.+++++.++++++.[---->++++.+[->++++.-[->+++-.-----[->+++.+++++.++++++.[---->++++.-[--->++-.[----->++-.[->+++.---------.-------------.[--->+---.+.---.----.-[->+++++-.-[--->++-.[----->+++.,[.,]+[--------->+++.-[--->+++.

Outputs a method like int d(int i){return i+42;} (which doesn't look like a Java method, but... Java!)

\$\endgroup\$
7
  • 1
    \$\begingroup\$ Y'know, because Java. \$\endgroup\$ Commented Nov 11, 2015 at 14:23
  • 1
    \$\begingroup\$ What means, it doesn't look like a Java method? How else should that kind of method look? \$\endgroup\$ Commented Nov 11, 2015 at 17:46
  • \$\begingroup\$ @PaŭloEbermann public static int method(int argument){return argument+42;} \$\endgroup\$
    – jado
    Commented Nov 11, 2015 at 17:48
  • 1
    \$\begingroup\$ Hmm, okay. (Though if you are programming only in public static, you are not really doing Java. argument would also better be named summand ;-). ) \$\endgroup\$ Commented Nov 11, 2015 at 17:50
  • \$\begingroup\$ @PaŭloEbermann I was just modelling public static void main(String[] arguments){} \$\endgroup\$
    – jado
    Commented Nov 11, 2015 at 17:51
3
\$\begingroup\$

POSIX shell to Haskell, 19 bytes

read n;echo "($n+)"

Anonymous functions being allowed, Haskell is a good output choice with the operator sections.

\$\endgroup\$
3
\$\begingroup\$

PHP → JavaScript (ES6), 20 24 bytes

Reading from STDIN is always expensive in PHP. It looks a bit strange:

x=>x+<?fgets(STDIN);

It prints x=>x+ and waits for user input to complete the string, terminates with the complete anonymous JavaScript function, e.g. x=>x+2.

First version (24 bytes)

<?='x=>x+'.fgets(STDIN);
\$\endgroup\$
5
  • \$\begingroup\$ Why not just x=>x+<?=$x;? It's valid PHP4.1 and you can pass the values over POST, GET, SESSION, COOKIE, ... and it will work flawlessly. Or on PHP5.3 with register_globals=on (on your php.ini file). \$\endgroup\$ Commented Nov 10, 2015 at 22:34
  • \$\begingroup\$ @IsmaelMiguel The requirements of the challenge say that it takes a number from STDIN. Every time I ignored that it was criticized. So I take requirements seriously now. :) \$\endgroup\$ Commented Nov 10, 2015 at 22:39
  • \$\begingroup\$ Well, I think that GET is passed over STDIN. I can test it in a while. \$\endgroup\$ Commented Nov 10, 2015 at 22:41
  • 1
    \$\begingroup\$ serverfault.com/questions/187025/… <-- Evidence on my claim. Just use the code I provided, slap this link and no one can complain \$\endgroup\$ Commented Nov 10, 2015 at 22:47
  • 1
    \$\begingroup\$ Ho, understood! That's clever, indeed! \$\endgroup\$
    – Blackhole
    Commented Nov 11, 2015 at 14:20
3
\$\begingroup\$

Retina to Pip, 4 bytes

Uses one file for each of these lines + 1 penalty byte; or, put both lines in a single file and use the -s flag.

$
+_

Matches the end of the input with $ and puts +_ there. This results in something of the form 3+_, which is an anonymous function in Pip.

\$\endgroup\$
3
\$\begingroup\$

Bash → C/C++/C#/Java, 33 bytes

and maybe others

echo "int f(int a){return a+$1;}"
\$\endgroup\$
3
\$\begingroup\$

Tiny Lisp to Ceylon, 68 61

(d u(q((n)(c(q(Integer x))(c(q =>)(c(c(q x+)(c n()))()))))))

Tiny Lisp doesn't have real input and output – it just has expression evaluation. This code above creates a function and binds it to u. You can then call u with the argument n like this: (u 7), which will evaluate to this Tiny Lisp value:

((Integer x) => (x+ 7))

This is a valid Ceylon expression, for an anonymous function which adds 7 to an arbitrary integer.

Thanks to DLosc for an improvement of 7 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ Nice job working with the very limited output capabilities! \$\endgroup\$
    – DLosc
    Commented Nov 10, 2015 at 22:28
3
\$\begingroup\$

JavaScript to Lambda Calculus, 39 bytes

(This uses the linked document as a basis.)

alert((x=>`λa(${x}(add a))`)(prompt()))

Say input is 5. Then this becomes:

"λa(5(add a))"
\$\endgroup\$
7
  • 1
    \$\begingroup\$ Where is an interpreter for Lambda Calculus? \$\endgroup\$
    – feersum
    Commented Nov 10, 2015 at 0:08
  • \$\begingroup\$ @feersum Check the link. I'm not sure if an actual interpreter exists, but I was told I was able to submit in this language. \$\endgroup\$ Commented Nov 10, 2015 at 0:10
  • 1
    \$\begingroup\$ What do you mean "you were told"? Answers that don't run in any language implementation are invalid. \$\endgroup\$
    – feersum
    Commented Nov 10, 2015 at 0:12
  • \$\begingroup\$ @feersum I asked; it has been done before. \$\endgroup\$ Commented Nov 10, 2015 at 0:14
  • 1
    \$\begingroup\$ Lambda calculus being fairly well known, I assume there must be a valid interpreter somewhere. The point is, you need to identify such an interpreter and write the code in the format accepted by that interpreter. \$\endgroup\$
    – feersum
    Commented Nov 10, 2015 at 0:17
3
\$\begingroup\$

Python 2 to CJam, 18 20 bytes

Thanks to LegionMammal978 for correcting the functionality.

print"{%f+}"%input()

The Python does a basic string format. %f is the code for a float, and since I wouldn't lose any bytes for handling floats, I went ahead and did so.

The CJam is much the same as the Golfscript->CJam answer. It looks something like this:

{7.4+}

or:

{23+}

It's a block that takes the top value off the stack, pushes the special number, then adds them.

\$\endgroup\$
2
  • \$\begingroup\$ @LegionMammal978 At the end of the question it says that anonymous functions and lambda expressions are allowed. Still, I'll edit my answer with an alternative full program. \$\endgroup\$
    – jqkul
    Commented Nov 13, 2015 at 3:06
  • 1
    \$\begingroup\$ The OP means that the output can be a function. \$\endgroup\$ Commented Nov 13, 2015 at 12:19
2
\$\begingroup\$

Microscript II to Javascript ES6, 9 bytes

"x=>x+"pF
\$\endgroup\$
2
\$\begingroup\$

GNU sed to C, 46 bytes

sed -r 's/^([0-9]+)$/f(int x){return x+\1;}/'
\$\endgroup\$
2
\$\begingroup\$

Vitsy to K, 5 Bytes

\o/ K will be being used very soon if it can do this.

N'+'Z

or maybe...

N'+'O

If the input is taken as a string (only for 0-9 input)...

i'+'Z

All of these, for input 2, will output:

2+
\$\endgroup\$
2
\$\begingroup\$

Ceylon to Tiny lisp, 76

shared void run(){print("(q((x)(s ``process.readLine()else""``(s 0 x))))");}

This produces (after reading a line of input) output like (q((x)(s 5(s 0 x)))), which evaluates in Tiny Lisp to ((x) (s 5 (s 0 x))), a function which takes an argument x, subtracts it from 0, and subtracts the result from 5. (Yeah, this is how one adds in Tiny Lisp, there is only a subtraction function build in. Of course, one could define an addition function first, but this would be longer.)

You can use it like this as an anonymous function:

((q((x)(s 5(s 0 x)))) 7)

(This will evaluate to 12.)

Or you can give it a name:

(d p5 (q((x)(s 5(s 0 x)))))
(p5 7)

Corrections and Golfing Hints from DLosc, the author of Tiny Lisp.

\$\endgroup\$
0
2
\$\begingroup\$

JaptTeaScript, 5 bytes

U+"+x

This is pretty simple.


Explanation

U+     // Input added to the string...
  "+x  // This is the string
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Japt → Japt would be not only a palindrome, but horizontally mirror-able: U+"+U ;) \$\endgroup\$ Commented Nov 23, 2015 at 2:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.