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

2
\$\begingroup\$

Fith to Forth, 28 bytes (non-competitive)

Fith was created after the challenge was posted.

": p " line " + ;" + + .

This pushes the first part, then reads the number, then pushes the last part. The two + words then concatenate the three strings, and . prints.

The output looks something like this:

: p 7 + ;

This defines a function named p, and as far as I know, it is the shortest method in Forth to define the necessary function.

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

Python 3 to 05AB1E, 14 bytes

lambda n:n+'+'

Try it online! Takes a string as input.

The Python function simply returns 'n+', where n is the input. In 05AB1E, pushes implicit input, push n, add them, and implicitly output.

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

Ruby to Myby, 14 bytes

$><<"+&"+$**""
C:\Myby>ruby -e "$><<'+&'+$**''" 3 > out  && myby -l out 3  
Nibbles: 4A 03
4 nibbles, 2 bytes
6

Myby is exactly J in this case, where one can bond an argument to a dyadic verb to create a monadic verb.

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

KnightSquire, 13 bytes

O+"\(x)=>x+"P

Attempt This Online!

Knighting this code will inquire thee, in which thou shalt enscribe numerals on thine keyboard. Whilst one can be a jest and use Arabic numerals, true royalties use Roman numerals, such as \$\text{CCCXIV}\$ (that's 314 for you jests). (It doesn't matter as it is string concatenation - Squire parses both).

Upon striking the return key, the following journey shall be proclaimed:

\(x)=>x+CCCXIV

Some foreigners might entitle this as an 'anonymous lambda' but true Squire users lack this brash peasant vocabulary.

Squire program

f=\(x)=>x+CCCXIV
# embark and proclaim
proclaim(f(X)) # CCCXXIV (314 + 10 = 414)
\$\endgroup\$
1
\$\begingroup\$

Chaîne to JavaScript ES6, 7 bytes

Again, assumes numeric input.

x=>x+{i
~~~~~~~
x=>x+   ; write that text
     {  ; command sequence
      i ; read input and push to stack
        ; implicit: take top entry on stack and pushes it to writing string
        ; implicit: close }
        ; implicit: print string
\$\endgroup\$
1
\$\begingroup\$

Powershell to EmacsLisp, 37 bytes

function p($a){"(defun p(x)(+ $a x)"}

EmacsLisp to Powershell, 44 bytes

(defun p(x)(format"function p($a){%s+$a}"x))
\$\endgroup\$
1
\$\begingroup\$

Ruby to Thue, 139 bytes

Not as short as i thought it should be, but I will post anyway. Thue may not be the best language for golfing in.

10.times{|n|puts "#{n}+::=#{n==9?'+':''}#{(n+1)%10}\n#{n}|::=|*#{n}\n>#{n}::=~#{n}"};puts "_::=:::\n||::=<\n<*::=<>\n::=\n|_#{'+'*($*[0]).to_i}|"

Takes a command-line augment in ruby, and regular input in Thue.

\$\endgroup\$
1
\$\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\$
1
  • \$\begingroup\$ @LegionMammal978 Done. Fixed it. \$\endgroup\$ Commented Nov 12, 2015 at 1:13
1
\$\begingroup\$

Python to C++, 49 bytes 39 bytes

print"[](int x){return x+%d;}"%input()

Thanks @Mego !

Basically, this prints out lambda with the argument x and adds it to the input of the Python program.

\$\endgroup\$
1
\$\begingroup\$

Python to Lisp - 34 36 Bytes

print"(lambda (x) (+ x %s))"%input()
\$\endgroup\$
1
\$\begingroup\$

Python3 → Go, 89 bytes

print('package main;import"fmt";func main(){a()};func a(x int)int{fmt.Println(x+'+input()+')}')

also a full program which compiles/runs with any of go build, go install or go run.

if we allow just a function (which is an error/will not compile) then 47 bytes:

print('func a(x int)int{return x+'+input()+'}')
\$\endgroup\$
1
\$\begingroup\$

Go → Python (any version), 109 bytes

package main;import"fmt";func main(){var a string;_,_=fmt.Scanln(&a);fmt.Println("def a(x):\n return x+"+a)}
\$\endgroup\$
1
\$\begingroup\$

Seriously to K, 4 bytes

'+,+

Everyone else is posting "to K" answers, I might as well too. Takes input in the form of a string ("3.14").

Try it online

\$\endgroup\$
1
\$\begingroup\$

Go → LOLCODE, 192 bytes

package main;import."fmt";func main(){a:="";Scanln(&a);Println("HAI1.3\nHOW IZ I ADDIN YR X\nI HAS A VAR\nVAR R SUM OF X AN "+a+"\nVISIBLE VAR\nIF U SAY SO\nKTHXBYE")}

the resulting output compiles with lci.

\$\endgroup\$
1
\$\begingroup\$

Java (OpenJDK 8) -> Python 3, 155 153 bytes

import java.util.Scanner;class Main{public static void main(String[] args){Scanner s=new Scanner(System.in);System.out.println("lambda x:x+"+s.next());}}

Try it online!

Not exactly a winning combination but it gave me a chance to practice using java.

Saved 2+ bytes thanks to @WheatWizard

\$\endgroup\$
5
  • \$\begingroup\$ You could probably save bytes by writing out to a python lambda function. "lambda x:x+". \$\endgroup\$
    – Wheat Wizard
    Commented Feb 21, 2017 at 0:21
  • \$\begingroup\$ @WheatWizard thanks \$\endgroup\$
    – sporkl
    Commented Feb 21, 2017 at 14:46
  • \$\begingroup\$ You don't need spaces on the sides of your equals signs add well. \$\endgroup\$
    – Wheat Wizard
    Commented Feb 21, 2017 at 14:55
  • \$\begingroup\$ Why did you remove the target language from the title? \$\endgroup\$ Commented Nov 8, 2019 at 12:31
  • \$\begingroup\$ @PaŭloEbermann I think this was my first or second answer on this site, so I didn’t really know what I was doing. It’s fixed now. \$\endgroup\$
    – sporkl
    Commented Nov 9, 2019 at 15:04
1
\$\begingroup\$

05AB1E to J, 4 bytes

„&+«

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Nibbles to Husk, 2 bytes (4 nibbles)

"+"$

enter image description here

Outputs the Husk function +2 for input 2.
Try the Husk function online


Husk to Nibbles, 4 bytes

:'+s

Try it online!

Outputs the Nibbles function +2 for input 2. Here's a Nibbles program that uses this function to add 2 to every elemnt of the input:

.$ +2
.$      # map over every element of the input
   +2   # using the function '+2'

enter image description here

\$\endgroup\$
1
\$\begingroup\$

Charcoal to Vyxal, 2 bytes

θ+

Try it online!

Takes input (for example 5) on stdin. Outputs 5+, which adds 5 to the input when run as vyxal.

\$\endgroup\$
1
\$\begingroup\$

C (gcc) to Python, 85 57 50 bytes

Borrowing @Oskar Skog's suggestions

main(n){scanf("%d",&n);printf("lambda m:m+%d",n);}

Python 3 to C (gcc), 43 bytes

print('i(p){printf("%d",p+'+input()+');}')

Extras (because why not)

1. C (gcc) to C (gcc), 66 bytes

main(n){scanf("%d",&n);printf("i(p){printf('%s',p+%d);}","%d",n);}

2. Python 3 to Python 3, 30 bytes

print(f"lambda n:n+{input()}")

\$\endgroup\$
2
  • \$\begingroup\$ This can be shortened to main(n){scanf("%d",&n);printf("def i(x):return x+%d",n);}. This is K&R C, the default data type is int and you don't need to declare int-returning functions. \$\endgroup\$
    – Oskar Skog
    Commented Aug 23, 2022 at 13:58
  • \$\begingroup\$ You can also use lambda which makes the output shorter as well. i=lambda x:x+y is a drop-in replacement, but I think the question allows removing i= as well. \$\endgroup\$
    – Oskar Skog
    Commented Aug 23, 2022 at 14:00
0
\$\begingroup\$

Pyth to C++, 32 bytes

%"[](int x){return x+%d;}"Q

Just a Pyth version of my Python one.

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

Python to Pyth, 20 bytes

print"L+%db"%input()

Basically, it prints out a lambda function (L) that takes in the argument (b, implicit variable) and adds the input from the Python program.

\$\endgroup\$
1
  • \$\begingroup\$ @BadCatEye fixed it.. \$\endgroup\$
    – TanMath
    Commented Nov 12, 2015 at 21:43
0
\$\begingroup\$

C# to Mathematica, 85 81 bytes

using C=System.Console;class C{static void Main(){C.Write(C.ReadLine()+"+#&");}}

Outputs a Mathematica Function of form

n+#&
\$\endgroup\$
4
  • \$\begingroup\$ In the first programming language (C# in your case) you should write a full program (which reads input and produces output), not just a single function. \$\endgroup\$ Commented Nov 11, 2015 at 19:21
  • \$\begingroup\$ @PaŭloEbermann "EDIT: Anonymous functions and lambda expressions are allowed!" \$\endgroup\$ Commented Nov 11, 2015 at 21:57
  • \$\begingroup\$ This is for the created function, not the creating one ... see my comment at the question about this and the OP's answer to it (which were both later than this edit). \$\endgroup\$ Commented Nov 11, 2015 at 22:00
  • \$\begingroup\$ Save 4 bytes by replacing WriteLine with Write \$\endgroup\$
    – Metoniem
    Commented Feb 20, 2017 at 9:47
0
\$\begingroup\$

BatchK, 8 bytes

echo;%1+

Fun fact: you don't have to use a space to separate echo from the text to be echo-ed, but you can pick from a whole series of different characters. For example: ., ,, :, ; and some others.

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

Java -> C (GCC) 144 134 bytes

class m{public static void main(String[]a){System.out.print(String.format("a(b){return %s+b;}",a[0]));}}

Takes the number(string, it is supposed to be a number, no error checking) as the first argument and prints the function to stdout.

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

Clojure -> JavaScript, 14 bytes

#(str"a=>a+"%)

An anonymous function in Clojure that returns an anonymous function in JavaScript (and potentially a few other languages). % is the implicit n.

(#(str"a=>a+"%) 3)

"a=>a+3"
\$\endgroup\$
0
\$\begingroup\$

Haskell -> Clojure, 25 bytes

\n->"#(+ % "++show n++")"

Anonymous Haskell function which takes n, and returns an anonymous Clojure function.

let f = \n->"#(+ % "++show n++")"

f 3

#(+ % 3)
\$\endgroup\$
0
\$\begingroup\$

Keg to Vyxal, 3 bytes

᠀\+

Try it online!

This takes the input at a string, pushes a plus sign, and outputs everything on the stack. It prints <input>+, which adds the input to a number in Vyxal.

\$\endgroup\$
1
  • \$\begingroup\$ Is the output a function in Vyxal, though? \$\endgroup\$
    – DLosc
    Commented Aug 15, 2022 at 15:44
0
\$\begingroup\$

dc to Haskell, 12 bytes

?[+)][(]nrnn

Try it online!

Could be -1 byte if input can be taken on the stack, instead of prompting the user.

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

QBIC to Jelly, 6 bytes

;?A+@+

Given n=3, this will output the Jelly code 3+. When another number is passed to this link, it will add 3 to it. Try that online!

The QBIC code:

;   Read cmd line param as A$
?A  PRINT A$
+@+ plus the string literal '+'
    The string literal is auto-closed by QBIC at EOF.
\$\endgroup\$
0
0
\$\begingroup\$

sB~ -> Befunge, 7 bytes

iN?N;`+

Explained:

i N 'input N
?N;`+ 'print N and "+"
\$\endgroup\$

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.