Inspired by "How to make pure functional JavaScript"

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

Inspired by "How to make pure functional JavaScript"
[Continued] Inspired by "How to make pure functional JavaScript"
[Python] How to make a list of character strings character by character
How to make a Japanese-English translation
How to make a slack bot
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make Blender scripts multilingual
How to make a crawler --Basic
django + nginx How to make images viewed only by logged-in users
How to make Word Cloud characters monochromatic
How to make Selenium as light as possible
[Python] How to make a class iterable
How to make multi-boot USB (Windows 10 compatible)
How to make a Backtrader custom indicator
How to make a Pelican site map
[Cocos2d-x] How to make Script Binding (Part 1)
How to make a dialogue system dedicated to beginners
[python] How to display list elements side by side
How to make an embedded Linux device driver (11)
How to convert m4a acquired by iTunes to wav
How to make WTForms TextArea correspond to file drop
How to make an embedded Linux device driver (8)
How to make Spigot plugin (for Java beginners)
How to make an embedded Linux device driver (1)
How to make multiple kernels selectable on Jupyter
How to make a dictionary with a hierarchical structure.
How to make an embedded Linux device driver (7)
How to make an embedded Linux device driver (2)
[Django] How to get data by specifying SQL.
How to make scrapy JSON output into Japanese
How to make an embedded Linux device driver (3)
How to erase the characters output by Python
[Blender x Python] How to make an animation
How to make an embedded Linux device driver (6)
How to make Substance Painter Python plugin (Introduction)
[Python] How to sort instances by instance variables
[Blender x Python] How to make vertex animation
How to make an embedded Linux device driver (5)
How to make an embedded Linux device driver (10)
How to make Python faster for beginners [numpy]
How to make Linux compatible with Japanese keyboard
How to make an embedded Linux device driver (9)
How to make AWS rekognition recognize local image files
Explain in detail how to make sounds with python
How to make a shooting game with toio (Part 1)
How to make unit tests Part.2 Class design for tests
How to make an interactive CLI tool in Golang
How to check the Java version used by Maven
How to make an HTTPS server with Go / Gin
How to list numbers by dividing them into n
How to save a table scraped by python to csv
Basics of PyTorch (2) -How to make a neural network-
How to make an embedded Linux device driver (12) (Complete)
How to use Decorator in Django and how to make it
How to unprefix the DB name used by pytest-django
How to handle JSON in Ruby, Python, JavaScript, PHP