Coder Social home page Coder Social logo

不动点组合子 about blog HOT 8 OPEN

CharLemAznable avatar CharLemAznable commented on August 30, 2024
不动点组合子

from blog.

Comments (8)

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Java实现Y组合子

import lombok.val;
import java.util.function.Function;

public class YCombinator {

    public static <T, R> Function<T, R> of(Function<Function<T, R>, Function<T, R>> f) {
        Function<Function, Function<T, R>> r = p -> {
            @SuppressWarnings("unchecked")
            val w = (Function<Function, Function<T, R>>) p;
            return f.apply(param -> w.apply(w).apply(param));
        };
        return r.apply(r);
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Java实现Y组合子 简化版

import java.util.function.Function;

public class YCombinator {

    public static <T, R> Function<T, R> of(Function<Function<T, R>, Function<T, R>> f) {
        return n -> f.apply(of(f)).apply(n);
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Go实现Y组合子

type RecFunc func(n interface{}) interface{}

func YComb(f func(RecFunc) RecFunc) RecFunc {
    var r = func(p interface{}) RecFunc {
        var w = p.(func(v interface{}) RecFunc)
        return f(func(param interface{}) interface{} {
            return w(w)(param)
        })
    }
    return r(r)
}

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Go实现Y组合子 简化版

type RecFunc func(interface{}) interface{}

func YComb(f func(RecFunc) RecFunc) RecFunc {
    return func(n interface{}) interface{} {
        return f(YComb(f))(n)
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Objective-C实现Y组合子

#define YCombinator(ARG_TYPE, RET_TYPE, REC_NAME, IMP_BLOCK)                    \
^RET_TYPE (^(RET_TYPE (^(^f)(RET_TYPE (^)(ARG_TYPE)))(ARG_TYPE)))(ARG_TYPE) {   \
    RET_TYPE (^(^r)(id))(ARG_TYPE) = ^(id p) {                                  \
        RET_TYPE (^(^w)(id))(ARG_TYPE) = p;                                     \
        return f(^RET_TYPE (ARG_TYPE param) {                                   \
            return w(w)(param);                                                 \
        });                                                                     \
    };                                                                          \
    return r(r);                                                                \
}(^RET_TYPE (^(RET_TYPE (^REC_NAME)(ARG_TYPE)))(ARG_TYPE) {                     \
    return IMP_BLOCK;                                                           \
})

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Objective-C实现Y组合子 简化版

#define YCombinator(NAME, ARG_TYPE, RET_TYPE)           \
typedef RET_TYPE(^NAME##RecFunc)(ARG_TYPE);             \
NAME##RecFunc NAME(NAME##RecFunc(^f)(NAME##RecFunc)) {  \
    return ^RET_TYPE(ARG_TYPE n) {                      \
        return f(NAME(f))(n);                           \
    };                                                  \
}

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Kotlin实现Y组合子

object YCombinator {

    fun <T, R> of(f: YFunction<YFunction<T, R>, YFunction<T, R>>): YFunction<T, R> {
        return object : YFunction<T, R> {
            override fun apply(t: T): R {
                return f.apply(of(f)).apply(t)
            }
        }
    }

    interface YFunction<in T, out R> : Function<R> {
        fun apply(t: T): R
    }
}

from blog.

CharLemAznable avatar CharLemAznable commented on August 30, 2024

Kotlin实现Y组合子 高阶函数

object YCombinator {

    fun <T, R> of(f: ((T) -> R) -> ((T) -> R)): (T) -> R {
        return { t: T -> f(of(f))(t) }
    }
}

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.