In the article "How to make pure functional JavaScript"
Purify your favorite language It can be a purely functional programming language.
There was. I tried it.
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)
Recommended Posts