LoginSignup
3
1

More than 3 years have passed since last update.

「純粋関数型JavaScriptのつくりかた」に触発されて

Last updated at Posted at 2019-06-03

「純粋関数型JavaScriptのつくりかた」の記事に

あなたの好きな言語を浄化して
純粋関数型プログラミング言語にすることができます。

とありました。
やってみました。

C#
using System;
class Pure{

  public static
  Func<object,Func<object>>
  pure=a=>()=>a;

  public static
  Func<Func<object>,
  Func<Func<object,Func<object>>,
  Func<object>>>
  bind=m=>f=>()=>f(m())();

  public static
  Func<Func<object>,object>
  exec=m=>m();

  public static
  Func<Func<object,object>,
  Func<object,Func<object>>>
  wrap=f=>a=>()=>f(a);
}

class MainClass{
  static void Main(){

    Func<object,Func<object>>
    put=Pure.wrap(a=>
    {Console.WriteLine(
    ((string)a).ToUpper());
    return a;});

    Func<object,Func<object>>
    get=Pure.wrap(x=>
    Console.ReadLine());

    Func<object>
    main=Pure.bind(get(null))(put);

    Pure.exec(main);
  }
}
Java
import java.io.*;
import java.util.function.*;

class Pure{

  public static
  Function<Object,Supplier<Object>>
  pure=a->()->a;

  public static
  Function<Supplier<Object>,
  Function<Function<Object,
  Supplier<Object>>,
  Supplier<Object>>>
  bind=m->f->
  ()->f.apply(m.get()).get();

  public static
  Function<Supplier<Object>,Object>
  exec=m->m.get();

  public static
  Function<Function<Object,Object>,
  Function<Object,
  Supplier<Object>>>
  wrap=f->a->()->f.apply(a);
}

class Main{

  static BufferedReader in=new
  BufferedReader(new
  InputStreamReader(System.in));

  static Object _get(){
    Object s=null;
    try{
      s=in.readLine();
    }catch(Exception e){
      e.printStackTrace();
    }
    return s;
  }

  public static void 
  main(String[]args){

    Function<Object,
    Supplier<Object>>
    put=Pure.wrap.apply(a->
    {System.out.println(
    ((String)a).toUpperCase());
    return a;});

    Function<Object,
    Supplier<Object>>
    get=Pure.wrap.apply(x->_get());

    Supplier<Object>
    main=Pure.bind.apply(
    get.apply(null)).apply(put);

    Pure.exec.apply(main);
  }
}
Python3
#pure=a=>()=>a;
pure=lambda a:lambda:a

#bind=m=>f=>()=>f(m())();
bind=lambda m:lambda f:lambda:f(m())()

#exec=m=>m();
exec=lambda m:m()

#wrap=f=>a=>()=>f(a);
wrap=lambda f:lambda a:lambda:f(a)

put=wrap(lambda a:print(a.upper()))

get=wrap(lambda x:input())

main=bind(get(""))(put)

exec(main)
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1