30
\$\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\$
  • \$\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 Nov 9 '15 at 6:12
  • \$\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\$ – Blake Lockley Nov 9 '15 at 6:32
  • 4
    \$\begingroup\$ I don't see any definition for function BUT that we are supposed to write.. -_- \$\endgroup\$ – Optimizer Nov 9 '15 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\$ – ETHproductions Nov 9 '15 at 15:31
  • \$\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\$ – Paŭlo Ebermann Nov 9 '15 at 20:40

51 Answers 51

23
\$\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\$
  • 1
    \$\begingroup\$ Which encoding are you using? \$\endgroup\$ – Conor O'Brien Nov 9 '15 at 15:30
  • 2
    \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ "The source code uses the CP437 encoding." \$\endgroup\$ – ETHproductions Nov 9 '15 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\$
  • \$\begingroup\$ I'm pretty sure there's a language where you don't need the close-quote. \$\endgroup\$ – lirtosiast Nov 9 '15 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 Nov 9 '15 at 6:06
13
\$\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\$
  • 2
    \$\begingroup\$ I was just going to post this! \$\endgroup\$ – Loovjo Nov 9 '15 at 6:36
  • \$\begingroup\$ Wow that's impressive. {n +} pushes 42, then executes +. (Probably should be {42 +} or "pushes n") \$\endgroup\$ – Justin Nov 11 '15 at 7:10
  • \$\begingroup\$ @Justin Indeed. Thanks! \$\endgroup\$ – Dennis Nov 11 '15 at 14:20
12
\$\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\$
10
\$\begingroup\$

Rotor to K, 2 bytes

'+

Might as well jump in on the K bandwagon.

\$\endgroup\$
10
\$\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\$
  • \$\begingroup\$ K has automatic currying, so you can just do i'++p. \$\endgroup\$ – kirbyfan64sos Nov 9 '15 at 15:51
  • \$\begingroup\$ The distance between K and O is 4. You need to shorten it one byte. \$\endgroup\$ – mbomb007 Nov 11 '15 at 22:39
  • \$\begingroup\$ @mbomb007 wat. The code is 5 bytes long \$\endgroup\$ – phase Nov 11 '15 at 22:42
  • 2
    \$\begingroup\$ The letters. In the alphabet. Get it? \$\endgroup\$ – mbomb007 Nov 11 '15 at 22:42
  • 1
    \$\begingroup\$ @mbomb007 that was too complicated for me \$\endgroup\$ – phase Nov 11 '15 at 23:05
9
\$\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\$
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\$
8
\$\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\$
  • \$\begingroup\$ Note that the .dat bytecounting is not applicable for this system, as the Minecraft version listed does not support structure files. \$\endgroup\$ – Addison Crump Feb 21 '17 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\$ – Redwolf Programs Nov 9 at 16:04
  • \$\begingroup\$ @RedwolfPrograms Not at the time of submission, four years ago. \$\endgroup\$ – Addison Crump Nov 10 at 4:44
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\$
  • \$\begingroup\$ Hey, someone actually went through with Seriously! :D \$\endgroup\$ – ETHproductions Nov 9 '15 at 15:26
  • 2
    \$\begingroup\$ Seriously? You finished Seriously before Simplex? D: \$\endgroup\$ – Conor O'Brien Nov 9 '15 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\$ – Mego Nov 9 '15 at 20:15
7
\$\begingroup\$

Mathematica to C#, 22 bytes

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

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

x=>x+n
\$\endgroup\$
  • 1
    \$\begingroup\$ The output is valid Javascript (ES6/7) as well. \$\endgroup\$ – Ismael Miguel Nov 10 '15 at 22:27
6
\$\begingroup\$

rs -> K, 2 bytes

/+

Live demo.

\$\endgroup\$
6
\$\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\$
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\$
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\$
  • \$\begingroup\$ Good job, I like this post. \$\endgroup\$ – phase Nov 11 '15 at 23:58
5
\$\begingroup\$

Haskell to Mathematica, 14 bytes

(++"+#&").show
\$\endgroup\$
5
\$\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\$
  • 1
    \$\begingroup\$ Y'know, because Java. \$\endgroup\$ – Conor O'Brien Nov 11 '15 at 14:23
  • 1
    \$\begingroup\$ What means, it doesn't look like a Java method? How else should that kind of method look? \$\endgroup\$ – Paŭlo Ebermann Nov 11 '15 at 17:46
  • \$\begingroup\$ @PaŭloEbermann public static int method(int argument){return argument+42;} \$\endgroup\$ – phase Nov 11 '15 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\$ – Paŭlo Ebermann Nov 11 '15 at 17:50
  • \$\begingroup\$ @PaŭloEbermann I was just modelling public static void main(String[] arguments){} \$\endgroup\$ – phase Nov 11 '15 at 17:51
4
\$\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\$
  • \$\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\$ – Ismael Miguel Nov 10 '15 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\$ – insertusernamehere Nov 10 '15 at 22:39
  • \$\begingroup\$ Well, I think that GET is passed over STDIN. I can test it in a while. \$\endgroup\$ – Ismael Miguel Nov 10 '15 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\$ – Ismael Miguel Nov 10 '15 at 22:47
  • 1
    \$\begingroup\$ Ho, understood! That's clever, indeed! \$\endgroup\$ – Blackhole Nov 11 '15 at 14:20
4
\$\begingroup\$

Pyth -> K, 4 bytes

+z\+

K is really easy to abuse here...

Live demo.

\$\endgroup\$
4
\$\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\$
  • \$\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\$ – bkul Nov 13 '15 at 3:06
  • 1
    \$\begingroup\$ The OP means that the output can be a function. \$\endgroup\$ – LegionMammal978 Nov 13 '15 at 12:19
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\$

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\$

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\$
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\$
  • \$\begingroup\$ Nice job working with the very limited output capabilities! \$\endgroup\$ – DLosc Nov 10 '15 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\$
  • 1
    \$\begingroup\$ Where is an interpreter for Lambda Calculus? \$\endgroup\$ – feersum Nov 10 '15 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\$ – Conor O'Brien Nov 10 '15 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 Nov 10 '15 at 0:12
  • \$\begingroup\$ @feersum I asked; it has been done before. \$\endgroup\$ – Conor O'Brien Nov 10 '15 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 Nov 10 '15 at 0:17
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\$

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\$
2
\$\begingroup\$

Javascript (ES6) > PHP/Javascript, 38 48 47 bytes

This code is an anonymous function that creates a VALID PHP and Javascript function called anonymous.

P=prompt;P(Function('$x',`return $x+${P()};`));

As of the writting of this code, only Firefox has implemented ES6 nativelly (without flags and suchs).

This code uses the prompt() function to request input and for output. Firefox supports copying the output of prompt(), making this a legitime output method for Javascript.


Considerations:

  • The ; at the end is required for PHP
  • The space in return $x is required by Javascript
    • Ignoring it will say that return$x is undefined
  • The function name is automatic: I have no choice over it
  • Picking only PHP or only Javascript will only reduce the code in 1 byte, and would reduce the fun-factor by half
  • May work with other languages

Old answer (48 bytes):

alert(Function('$x',`return $x+${prompt()};`));

Old invalid answer:

y=>''+Function('$x',`return $x+${y};`)
\$\endgroup\$
  • \$\begingroup\$ @LegionMammal978 Done. Fixed it. \$\endgroup\$ – Ismael Miguel Nov 12 '15 at 1:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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