10
\$\begingroup\$

Task

I need a function that accept string and return a function f1.

That f1 function should also accept string and return a f2 function, but as a side effect it must print string passed to previous function.

That f2 function should also accept string and return a f3 function, but as a side effect it must print string passed to previous function.

And so on.

For language that require explicit declaration of side effects (like haskell) you may return next best thing (like IO(...) in haskell).

For language that don't have function as first class object (like Java) you may return next best thing (like instance of Function interface in Java).

You need to show function and example of it usage.

This is code golf all standard rules apply. Only function itself is counted in score. Example is for demonstration only.

Reference implementation in Java

    static Function<String, Function> f(String s) {
        class C implements Function<String, Function> {
            String s1;

            public C(String s) {
                s1 = s;
            }

            @Override
            public Function apply(String s) {
                System.out.println(s1);
                return new C(s);
            }
        }
        return new C(s);
    }

Example of usage

        Function<String, Function> t = f("s1");
        t = t.apply("s2"); // Prints "s1"
        t = t.apply("s3"); // Prints "s2"
\$\endgroup\$
4
  • 1
    \$\begingroup\$ if f1 returns a new function f2, how is the function remember "its" past life. :( \$\endgroup\$
    – tsh
    Commented Sep 13 at 11:27
  • \$\begingroup\$ @tsh You right. This question doesn't make sense. I'll remove it. \$\endgroup\$
    – talex
    Commented Sep 13 at 12:32
  • \$\begingroup\$ Can we print to stderr instead of stdout? \$\endgroup\$
    – cobrexus
    Commented Sep 16 at 11:47
  • \$\begingroup\$ @bzr Yes. I think it is part of standard rules. \$\endgroup\$
    – talex
    Commented Sep 16 at 11:53

14 Answers 14

12
\$\begingroup\$

JavaScript (V8), 21 bytes

f=s=>x=>f(x,print(s))

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ This is way simpler than my Java solution. \$\endgroup\$
    – talex
    Commented Sep 13 at 6:40
  • \$\begingroup\$ By looking at the code, I haven't noticed that it prints before running it myself. I love this. \$\endgroup\$ Commented Sep 15 at 19:12
7
\$\begingroup\$

K (ngn/k), 9 bytes

partial application ftw

{ \x;o@y}

Try it online!

 f:{ \x;o@y}
 f:f"s1"
 f:f"s2"
"s1"
 f:f"s3"
"s2"
\$\endgroup\$
6
\$\begingroup\$

R, 24 bytes

f=\(s)\(t){show(s);f(t)}

Attempt This Online!

\$\endgroup\$
6
\$\begingroup\$

Java, 85 bytes

interface N{N f(String s);static N i=s->t->{System.out.println(s);return N.i.f(t);};}

Try it online.

Explanation:

interface N{     // Interface to use as our lambda
  N f(String s); //  taking a String parameter, and returning another instance of itself

  static         // Static context so we're able to reference the interface
         N i=    // Create this interface as re-usable lambda method:
    s->          //  Taking a String parameter and returning another lambda method:
       t->{      //   Also taking a String parameter and returning a lambda method:
         System.out.println(s);
                 //    Print the String of the outer lambda with trailing newline
         return  //    And then return
           N.i.f(t);};}
                 //     A call to the outer lambda with the inner String parameter,
                 //     using a static call to prevent 'self-reference in initializer' errors
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Why don't you put i inside N. This way you don't need to depend on outer class name. Which look little bit sketchy. \$\endgroup\$
    – talex
    Commented Sep 13 at 7:50
  • \$\begingroup\$ And you missed ; at the end of definition of i. \$\endgroup\$
    – talex
    Commented Sep 13 at 7:51
  • \$\begingroup\$ @talex You're completely right about putting it inside the interface, fixed. (As for the missing ;, those usually aren't counted with pure lambda answers, hence why I put it at the end. But when it's used within the interface, it's indeed required.) \$\endgroup\$ Commented Sep 13 at 7:57
  • \$\begingroup\$ "usually aren't counted with pure lambda answer" I allow it. Java need every bit of advantage in those contests :) \$\endgroup\$
    – talex
    Commented Sep 13 at 8:02
5
\$\begingroup\$

Arturo, 33 bytes

f:$->x->$.import:->x=>[f&print,x]

Try it!

Ungolfed:

f: function [x][
    function .import:[x] [y][
        print x
        f y
    ]
]
\$\endgroup\$
4
\$\begingroup\$

Ruby, 23 bytes

f=->s{->z{puts s;f[z]}}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

C (gcc), 27 bytes

t;f(s){t&&puts(t);t=s;s=f;}

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ I smell something shady :) You aren't returning function that print previous result. If I call returned function again I will get different result. That was same function all along. C don't play by the rules, I guess :) \$\endgroup\$
    – talex
    Commented Sep 13 at 14:25
  • \$\begingroup\$ @talex It is very shady, but I think it does what the OP wanted. \$\endgroup\$
    – jdt
    Commented Sep 13 at 14:32
  • \$\begingroup\$ Yes. OP allows it :) I never explicitly required the function to work more than once. Is it common loophole? If not you may want to mention it in your answer. \$\endgroup\$
    – talex
    Commented Sep 13 at 14:36
  • \$\begingroup\$ Just to satisfy my curiosity: I know that C functions always returns something, even without return, but why it returns pointer to itself? \$\endgroup\$
    – talex
    Commented Sep 13 at 14:39
  • 2
    \$\begingroup\$ @talex, see this \$\endgroup\$
    – jdt
    Commented Sep 13 at 14:48
4
\$\begingroup\$

Wolfram Language (Mathematica), 19 18 bytes

f@a_=f[Print@a;#]&

Try it online!


(subjectively) more interesting:

24 bytes:

Curry[Print@@##2;#0]@!#&

Try it online!

27 bytes:

#0@*(D@@(#/._?Print@_->$))&

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 57, 45 bytes

Solution using a constructor:

data U=U([Char]->IO U);f s=U$(<$putStrLn s).f

Try it online!

Invalid solution using typeclasses:

{-#LANGUAGE FlexibleInstances#-}
class F a where g::String->a
instance F()where g _=()
instance F a=>F(String->IO a)where g a b=g b<$putStrLn a

Try it online!

The the typeclass based version will eventually return IO (), so it isn't really infinite. The non-typeclass based version however does go on forever.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Two function for a price of one is bargain :) I doubt it even possible to do it with one function because type of such function will be infinite. \$\endgroup\$
    – talex
    Commented Sep 13 at 19:40
  • \$\begingroup\$ @talex I found a solution using type classes that only uses 1 function if FlexibleInstances is enabled. Is my previous solution still acceptable given that this is possible? Try it online! \$\endgroup\$ Commented Sep 14 at 14:55
  • \$\begingroup\$ You can have both solution in your answer. Explain the difference between them. It would be fair to score proper solution though. \$\endgroup\$
    – talex
    Commented Sep 14 at 15:18
3
\$\begingroup\$

PARI/GP, 21 bytes

f(x)=t->print(x);f(t)

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

Python 3, 38 bytes

f=lambda x:lambda y:[f(y),print(x)][0]

Try it online!

Well sadly, the anonymous function can’t be shorter while being completely anonymous… 😮‍💨

\$\endgroup\$
2
  • \$\begingroup\$ 37 bytes. \$\endgroup\$
    – Neil
    Commented Sep 13 at 9:28
  • 8
    \$\begingroup\$ 35 bytes: f=lambda x:lambda y:print(x)or f(y) \$\endgroup\$
    – tsh
    Commented Sep 13 at 11:28
2
\$\begingroup\$

Clojure, 25 bytes

defn f[s]#(do(pr s)(f %))

Try it online!

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

C++, 69 bytes

auto f(auto s){return[=](this auto t,auto n){return cout<<s,s=n,t;};}

Try it online!

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

Rust, 57 49 45 29 bytes

fn x(s:&str)->!{dbg!(s);x(s)}

Attempt This Online!

\$\endgroup\$
0

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.