编写一个程序,用另一种语言编写功能但!


30

编写最短的程序,该程序从STDIN(或等效项)获取一个输入(n),并输出一个带有一个返回x + n的参数(x)的简单递增函数,但该函数必须使用另一种语言。很简单!

这是代码高尔夫球,适用常规规则,最短程序获胜。

示例:> <>到Python(非高尔夫)

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

输入:

3

输出:

def i(x):
    return x+3

编辑:允许匿名函数和lambda表达式!


输入可以达到多少?据我所知,您的示例仅对其中>>一半使用一位数字。
Sp3000

从理论上讲,in应该能够使用任何(合理的)输入,但是仅使用可以保持1位数字的输入的答案是完全可以接受的,我确实考虑过重新编写示例以更正此问题,但是我认为为了简单起见,请不要使用它。
布莱克·洛克利(Brake Lockley),2015年

4
我看不到function BUT我们应该写的任何定义。-_-
Optimizer

1
当一个函数返回另一个函数时,称为闭包。我不知道这是否适用于跨语言...
ETHproductions'Nov

@ETHproductions我猜目标不是返回函数对象,而是返回其他语言的函数源代码。
圣保罗Ebermann

Answers:


23

GS2 →K,2个字节

•+

这将打印出默认的单调函数。源代码使用CP437编码。在线尝试!

测试运行

$ 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

怎么运行的

GS2

  • GS2自动从STDIN读取并将输入压入堆栈。

  • 指示下一个字节是单例字符串文字。

  • 在退出之前,GS2将打印所有堆栈项目。

ķ

左参数currying在K中是自动的。

在此,通过n+将其二元函数+的left参数设置为,将其转换为一元函数n


1
您使用哪种编码?
Conor O'Brien 2015年

2
@CᴏɴᴏʀO'Bʀɪᴇɴ“源代码使用CP437编码。”
ETHproductions 2015年

16

ShapeScript →J,4个字节

"&+"

这会打印一个默认的单调动词。在线尝试:ShapeScriptJ

测试运行

$ 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

怎么运行的

ShapeScript

  • ShapeScript自动从STDIN读取并将输入推入堆栈。

  • "&+" 将该字符串压入堆栈。

  • 退出之前,ShapeScript将打印所有堆栈项目。

Ĵ

& 执行参数currying。

在这里,通过n&+将其二元动词+的左参数设置为,将其变成一元动词n


我敢肯定,有一种语言不需要用引号引起来。
lirtosiast 2015年

您很有可能是对的,但我无法回忆起带有隐式输入和隐式输出的情况。
丹尼斯

13

GolfScript→CJam,4个字节

{+}+

这将打印一个代码块(匿名函数)。在线尝试:GolfScriptCJam

测试运行

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

怎么运行的

高尔夫脚本

  • GolfScript自动从STDIN读取并将输入推入堆栈。

  • {+} 将该块压入堆栈。

  • + 执行级联,愉快地级联字符串和块。

  • 退出之前,GolfScript将打印所有堆栈项目。

贾姆

{n +}是一个代码块,执行该代码块时,首先将n其压入堆栈,然后执行+,从堆栈中弹出两个整数并压入它们的和。


2
我只是要发布这个!
Loovjo 2015年

哇,真令人印象深刻。{n +}推动42,然后执行+。(可能应该是“ {42 +}或” n
贾斯汀

@贾斯汀确实。谢谢!
丹尼斯

12

BrainF ***至JavaScript ES6,57字节

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

(假设输入由数字字符组成)

1337是您的意见。然后,将其编译为:

x=>x+1337


10

O至K,5个字节

我++

谢谢 @ kirbyfan64sos

创建挑战后添加功能的另一个版本。

我+
  • 获取输入,推送到堆栈
  • 将“ +”作为字符串插入
  • 输出堆栈内容

K具有自动计算功能,因此您可以执行i'++p
kirbyfan64sos

K和O之间的距离为4。您需要将其缩短一个字节。
mbomb007

@ mbomb007扫管。该代码长5个字节
阶段

2
这些信。在字母表中。得到它?
mbomb007

1
@ mbomb007对我来说太复杂了
阶段

9

R to Julia,19个字节

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

这会使用从STDIN中读取一个整数,scan()并使用将未命名的Julia函数写入STDOUT中cat()。Julia函数就是x->x+nn它来自R程序。


9

Malbolge到JavaScript ES6,71个字节

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

生成Malbolge代码总是很有趣。


8

Minecraft 1.8.7至K,7 6 + 33 + 27 + 62 = 129128字节

这是使用此版本的字节计数

the system

命令块(从左到右):

计分板目标加K虚拟
计分板球员设置JK <输入>
tellraw @a {得分:{名称:“ J”,目标:“ K”},附加:[{text:“ +”}]}

可能需要打更多的球,但这很简单:J用目标生成一个变量并将该目标K的得分设置为输入(没有STDIN-我认为这已经足够接近了)。然后,蜱后,输出变量的得分J为目标K,接着一个+。十分简单。


注意的.dat bytecounting是不是适用于该系统中,上市不支持结构文件的Minecraft版本。
艾迪生·克鲁普

对于第二个,您是否不能删除extra,将score JSON放入数组,然后在其后放置字符串?tellraw @a [{score:{name:"J",objective:"K"}},"+"]
Redwolf节目

@RedwolfPrograms四年前还没有提交。
Addison Crump

8

认真对待 Python,15个字节

,"lambda n:n+"+

期望输入为字符串形式,即 "3"

说明:

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

在线尝试(您必须手动输入输入内容,因为永久链接不喜欢引号)


嘿,实际上有人认真地经历了!:D
ETHproductions 2015年

2
认真吗 您在Simplex之前认真完成了吗?D:
Conor O'Brien 2015年

1
@CᴏɴᴏʀO'Bʀɪᴇɴ它尚未完全完成(请参阅问题跟踪器),但它足以在某些高尔夫球中使用。
Mego



6

Pyth到APL,7个 5字节

+z"--

Pyth代码只是将输入(z)与字符串连接起来"--"。这会在APL中创建一个未命名的单轨火车,其形式为n--n来自Pyth。在APL中调用该(n--)x参数时x计算n--x = n-(-x) = n+x

尝试:PythAPL

感谢Dennis,节省了2个字节!


5

> <>到Python,25 + 3 = 28字节

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

通过-v标志接受输入,例如

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

并输出Python lambda,例如lambda x:x+27

作为奖励,这是30字节的STDIN输入版本:

i:0(?v
x+"r~/"lambda x:
o;!?l<

5

鼠标转Ruby,19个字节

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

取消高尔夫:

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


Good job, I like this post.
phase


5

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


1
Y'know, because Java.
Conor O'Brien

1
What means, it doesn't look like a Java method? How else should that kind of method look?
Paŭlo Ebermann

@PaŭloEbermann public static int method(int argument){return argument+42;}
phase

1
Hmm, okay. (Though if you are programming only in public static, you are not really doing Java. argument would also better be named summand ;-). )
Paŭlo Ebermann

@PaŭloEbermann I was just modelling public static void main(String[] arguments){}
phase

4

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

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

@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. :)
insertusernamehere

Well, I think that GET is passed over STDIN. I can test it in a while.
Ismael Miguel

1
serverfault.com/questions/187025/… <-- Evidence on my claim. Just use the code I provided, slap this link and no one can complain
Ismael Miguel

1
Ho, understood! That's clever, indeed!
Blackhole


4

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.


@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.
bkul

1
The OP means that the output can be a function.
LegionMammal978

3

POSIX shell to Haskell, 19 bytes

read n;echo "($n+)"

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


3

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.


3

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

and maybe others

echo "int f(int a){return a+$1;}"

3

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+

3

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.


Nice job working with the very limited output capabilities!
DLosc

3

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

1
Where is an interpreter for Lambda Calculus?
feersum

@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.
Conor O'Brien

1
What do you mean "you were told"? Answers that don't run in any language implementation are invalid.
feersum


1
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.
feersum


2

GNU sed to C, 46 bytes

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

2

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.


2

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

@LegionMammal978 Done. Fixed it.
Ismael Miguel
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.