Tulis fungsi [tertutup]


15

Sejujurnya aku tidak percaya tantangan ini belum ada.

Tantangan

Tulis fungsi.

Spesifikasinya

  • Program Anda harus mendefinisikan semacam fungsi yang bisa dipanggil. Ini termasuk apa pun yang umumnya dikenal sebagai fungsi, fungsi lambda, atau subrutin. Semua jenis callable ini akan disebut sebagai "fungsi" dalam posting ini.

    1. Input ke fungsi adalah opsional dan tidak diperlukan.

    2. Nilai kembali dari fungsi juga opsional dan tidak diperlukan tetapi kontrol harus kembali ke program panggilan.

  • Fungsi tersebut harus ditetapkan ke beberapa jenis variabel sehingga memungkinkan untuk diakses di lain waktu. Ini termasuk penugasan tidak langsung (dalam sebagian besar bahasa yang umum di mana mendeklarasikan fungsi bernama secara otomatis menambahkan nama ke dalam cakupan saat ini) dan penugasan langsung (menugaskan fungsi anonim ke variabel secara langsung).

  • Fungsi tidak perlu disebutkan namanya.

  • Fungsi harus dibuat oleh Anda - Anda tidak bisa hanya menetapkan fungsi default dari bahasa ke variabel.

  • Tidak ada celah standar , silakan.

Ini adalah , jadi skor terendah dalam byte menang.


Komentar bukan untuk diskusi panjang; percakapan ini telah dipindahkan ke obrolan .
DJMcMayhem

3
Saya tidak mengerti mengapa ini dibuka kembali ketika tidak ada alasan ditutup pada awalnya, hanya disembunyikan oleh mod dalam obrolan
Jo King

2
Juga, sejak kapan Anda dapat memilih untuk membuka kembali posting Anda sendiri ??
Jo King

7
Anda harus menjadi pengacara daripada programmer untuk bersaing dalam tantangan ini.
anatolyg

2
Seharusnya ini tidak dibuka kembali dalam keadaan saat ini.
Mego

Jawaban:



21

Jelly , 0 byte

Cobalah online!

Tautan monadik yang mengembalikan argumennya. Karena ini adalah fungsi pertama yang muncul dalam skrip, ia dapat dipanggil menggunakan1Ŀ .

Terima kasih kepada @ lirtosiast untuk menunjukkan bahwa tautan / fungsi 0 byte akan berfungsi di Jelly.

Yaitu


3,4,5 1Ŀ

Cobalah online!


Jawaban 0 byte seharusnya berfungsi.
lirtosiast

@KevinCruijssen ya! Tempat yang bagus
Nick Kennedy

@NickKennedy Anda dapat menggunakan <pre><code>...</code></pre>untuk menjaga spasi / baris utama / baris baru di blok kode. Saya telah mengedit jawaban Anda sesuai dengan itu. :)
Kevin Cruijssen

@KevinCruijssen terima kasih!
Nick Kennedy

Pengetahuan saya tentang jelly cukup tipis tetapi tidakkah Anda memerlukan baris baru untuk mengakhiri tautan? Tampaknya digunakan dalam contoh.
Posting Rock Garf Hunter

13

Javascript, 6 byte

f=_=>0

Termasuk tugas variabel. Tidak banyak yang bisa dilihat di sini.


28
To make it looking more emoji-like: o=_=>o
Daniil Tutubalin

3
Another emoji d=_=>b
tsh

3
@tsh, alas, b is not defined in this case.
Daniil Tutubalin

2
@DanielO: One could certainly argue that. But in my opinion, one could even more effectively argue that you can't call it, so it's not a function. In javascript, a function call is unambiguously represented with parentheses.
recursive

2
@DaniilTutubalin But this only matters if you invoke it. And there is no such requirement about the function whether should run without throwing an exception.
tsh


8

ZX Spectrum BASIC, 6 bytes

DEF FN f()=PI

Hex dump: CE 66 28 29 3D A7. CE is a 1-byte keyword for DEF FN (including the trailing space), while A7 is a 1-byte keyword for PI. Call using FN f(). Example program:

  10 PRINT FN f(): DEF FN f()=PI

Output:

3.1415927

8

Haskell, 3 bytes

o=9

This code defines a polymorphic function called o which takes one type parameter and one typeclass instance parameter. When this function is called, it takes the given typeclass instance, gets its fromInteger member, calls that member with the Integer value for 9, and returns the result.

Granted, what I just described is merely the behavior of the Haskell function 9, and my code merely defines a function called o which is equivalent to 9.

Now the only question is, is the 9 function "created by you," or is it "a default function from the language"?

I think that it is "created by you." My reason for saying this is that if you read the specification for Haskell, you will (I assume) find no mention of a 9 function anywhere. Instead, the specification states that you can create a number literal by stringing together one or more digits. Therefore, by writing a string of digits, I have written a function—even if I just so happen to have only used one digit.


clever, type level functions
Mega Man

8

R, 9 bytes

body(t)=0

Try it online!

I think this complies with the rules. The function t takes no input and outputs 0. This works because there already exists a function called t (the transposition function) and it redefines the body of the function; it would not work with say body(a)=0 (no object called a) or body(F)=0 (F is a logical, not a function). I think it complies because it is still created by me: I am not reusing what the pre-defined function does, simply its name.

I don't think I've ever seen this used by R golfers, but there may be situations where it allows us to save a few bytes on challenges where we need a helper function.

A more standard solution would have been:

R, 13 bytes

f=function()0

Try it online!

Function which takes no input and outputs 0. This is 1 byte shorter than the function which takes no input and outputs nothing, which would be

f=function(){}

If we try to define a function with no body (f=function()), R interprets this as an incomplete command (this might not be true in older versions of R).

As pointed out by OganM, we take this down to 11 bytes with

R, 11 bytes

function()0

Try it online!

which technically complies with the challenge requirement that the function be assigned to some sort of variable, since it is (ephemerally) assigned to .Last.value.


1
function()0 should work for your second answer since the function need not be named. Neat trick on body<-, I've tried to use body and the like to do some of the weird challenges to mess with the language
Giuseppe

1
@Giuseppe "The function must be assigned to some sort of variable", so I don't think function()0 complies of the rules of this challenge. I'd be happy to give a bounty to an answer which uses the body()=" trick successfully.
Robin Ryder

4
function()0 would be assigned to .Last.value() though that would be pushing it
OganM

@OganM Nice point!
Robin Ryder

pryr::f(x) if we allow pryr.
qwr


6

C (gcc), 5 bytes

Defines a function f that takes no arguments and technically returns an undefined integer value.

f(){}

Try it online!


1
"takes no arguments" — lies badly. That's one of common misunderstandings about C: empty argument list means undefined amount of undefined type arguments without portable way to access them. As a sidenote, this is also C/C++ incompatibility.
val says Reinstate Monica


6

[Wolfram Language (Mathematica)], 1 byte

This one is slightly questionable:

f

Defines f, which can be "called" e.g. by f[], which "returns" the expression f[]


1
Hey, it can be called, and the return value is optional. This counts.
connectyourcharger

1
If this solution is acceptable, then the zero-byte answer is just as good: your "definition" of f doesn't do anything (apart from remembering "I've seen f") and can be left out. You can call f[] nonetheless, still returning unevaluated f[]. However, in any case you're mostly playing tricks with the pattern-replacer and not instructing to evaluate a function.
Roman

@Roman I've considered adding the zero byte version, but in the end I felt like that's even more questionable: this actually creates the symbol Global`f, while the empty version doesn't do that (you could argue that Null is assigned to %1, but Null is a built-in "function"). But as I've noted in the answer, whether the one byte solution is valid is also not entirely clear...
Lukas Lang


5

Lua, 8 bytes

f=load''

Try it online!

Defines a (global) function f.

This uses Lua load function to compile given string which happens to be empty in our case (empty code is valid code) into function which does exactly what we wrote in its body: nothing.

For ones wondering, standard solution would be

function f()end

but this is longer (15 bytes).


5

POSIX sh, 6 bytes

s()(1)

Using curly braces requires one more character.


5

Java, 10 Bytes

this should match the rules of the challenge

void f(){}

1
I'm pretty sure f=a->a; is valid as well. :)
Kevin Cruijssen

@Kevin Cruijssen I am no Java expert and I never used the Java array notation. How could I make your solution getting compiled? I initially "tested" my method in this TIO and then appended your approach there. Now, the compiler expects an identifier. Any explanation or wrong usage by me?
pixma140

2
It's a Java 8+ lambda function. So either of these two would work in this case. Here a more in depth explanation of Java 8+ lambdas in case you aren't familiar with them yet.
Kevin Cruijssen




4

C (gcc), 14 13 bytes

(*f)()=L"Ã";

Try it online!

This defines a function f returning int and accepting an unspecified number (and type) of parameters, the machine code of which is contained within the string literal. The unicode character à (stored in memory as 0xc3 0x00 0x00 0x00 on a little endian machine) corresponds to the x86 ret instruction that returns from the function. Non x86 architectures may require different opcode(s) to return.

gcc may require the -zexecstack flag to avoid a segfault.



3

Tcl, 6 5 11 bytes

set f {_ ;}

Try it online!

Including the assignment to the variable f as part of the bytecount to comply with rules. With this change, the more conventional definition below ties the one above for bytecount:

proc f _ {}

Is this a named function? The function must be assigned to some sort of variable so that it is possible to be accessed at a later time.
mbomb007

@mbomb007 I see your point, and fixed it accordingly
SmileAndNod

3

XSLT, 134 bytes

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template name="a"></xsl:template></xsl:stylesheet>

A template is the closest thing this language has to a function. It can definitely be called; it takes zero arguments and "returns" the empty string.






2

AppleScript, 10

on a()
end

Explained, compiled, and including invocation:

on a()    -- declare event handler "a"
end a     -- end declaration

-- invoke it:
a()


2

SmileBASIC (>=3), 9 bytes

DEF A
END

Function is called by A.


Alternatively DEF A*END in SB4
12Me21

2

Wolfram Language (Mathematica), 2 bytes

#&

Try it online!

Unfortunately, just & does not work (an anonymous function that does nothing).


You need to save the function to a variable according to the rules, e.g. f=#&
Lukas Lang

@LukasLang it's automatically saved to %1 so there's no need for an explicit assignment to a variable like f.
Roman

Good point, that should be enough to fulfil the rules
Lukas Lang
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.