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

1
2
2
\$\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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

JaptTeaScript, 5 bytes

U+"+x

This is pretty simple.


Explanation

U+     // Input added to the string...
  "+x  // This is the string
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Japt → Japt would be not only a palindrome, but horizontally mirror-able: U+"+U ;) \$\endgroup\$ – ETHproductions Nov 23 '15 at 2:47
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.

| improve this answer | |
\$\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
| improve this answer | |
\$\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))
| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Pyth to C++, 32 bytes

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

Just a Pyth version of my Python one.

| improve this answer | |
\$\endgroup\$
1
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @BadCatEye fixed it.. \$\endgroup\$ – TanMath Nov 12 '15 at 21:43
1
\$\begingroup\$

Python to Lisp - 34 36 Bytes

print"(lambda (x) (+ x %s))"%input()
| improve this answer | |
\$\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()+'}')
| improve this answer | |
\$\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)}
| improve this answer | |
\$\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

| improve this answer | |
\$\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.

| improve this answer | |
\$\endgroup\$
1
\$\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+#&
| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Paŭlo Ebermann Nov 11 '15 at 19:21
  • \$\begingroup\$ @PaŭloEbermann "EDIT: Anonymous functions and lambda expressions are allowed!" \$\endgroup\$ – LegionMammal978 Nov 11 '15 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\$ – Paŭlo Ebermann Nov 11 '15 at 22:00
  • \$\begingroup\$ Save 4 bytes by replacing WriteLine with Write \$\endgroup\$ – Metoniem Feb 20 '17 at 9:47
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

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You could probably save bytes by writing out to a python lambda function. "lambda x:x+". \$\endgroup\$ – Ad Hoc Garf Hunter Feb 21 '17 at 0:21
  • \$\begingroup\$ @WheatWizard thanks \$\endgroup\$ – Comrade SparklePony Feb 21 '17 at 14:46
  • \$\begingroup\$ You don't need spaces on the sides of your equals signs add well. \$\endgroup\$ – Ad Hoc Garf Hunter Feb 21 '17 at 14:55
  • \$\begingroup\$ Why did you remove the target language from the title? \$\endgroup\$ – Paŭlo Ebermann Nov 8 '19 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\$ – Comrade SparklePony Nov 9 '19 at 15:04
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.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

QBIC to Jelly, 6 bytes (NC)

;?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.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ This answer is non-competing, as QBIC and Jelly are newer than this challenge. \$\endgroup\$ – Erik the Outgolfer Feb 20 '17 at 13:47
  • \$\begingroup\$ @EriktheOutgolfer Thanks. hadn't noticed the date of the challenge. Marked it as NC. \$\endgroup\$ – steenbergh Feb 20 '17 at 13:51
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.

| improve this answer | |
\$\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"
| improve this answer | |
\$\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)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

sB~ -> Befunge, 7 bytes (non-competing)

iN?N;`+

Explained:

i N 'input N
?N;`+ 'print N and "+"
| improve this answer | |
\$\endgroup\$
1
2

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.