Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java works ... #6

Open
yii-iiy opened this issue Oct 8, 2023 · 1 comment
Open

Java works ... #6

yii-iiy opened this issue Oct 8, 2023 · 1 comment

Comments

@yii-iiy
Copy link
Collaborator

yii-iiy commented Oct 8, 2023

To fix the generic val unsupported

final var birds = 
new Object() 
{
	<A> Function<A,A> idiot () { return (Function<A,A>) (a -> a) ;}
} ;

birds.idiot().apply(7) ; // 7

method is value.

and, iife, also can use this.

@yii-iiy yii-iiy changed the title tmp ... Java works ... Oct 8, 2023
@yii-iiy
Copy link
Collaborator Author

yii-iiy commented Oct 8, 2023

还有这俩也记一下,应该有地方用到

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class Single {
    private static Map<Class<?>, Object> instances = new HashMap<>();

    public static <T> T instance(Class<T> clazz) throws Exception {
        if (!instances.containsKey(clazz)) {
            instances.put(clazz, clazz.getDeclaredConstructor().newInstance());
        }
        return (T) instances.get(clazz);
    }

    public static <T, R> Function<T, R> memoize(Function<T, R> function) {
        Map<T, R> cache = new HashMap<>();
        return input -> cache.computeIfAbsent(input, function::apply);
    }
}

public class A {
    public String b = "Hello, world!";
}

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Single.instance(A.class).b);  // 输出:Hello, world!

        Function<Integer, Integer> square = x -> x * x;
        Function<Integer, Integer> memoizedSquare = Single.memoize(square);
        System.out.println(memoizedSquare.apply(5));  // 输出:25
    }
}
<T, R> Function<Function<T, R>, Function<T, R>> memoize = <T, R> (
    
    f -> { Map<T, R> memoizes = new HashMap<>(); return input -> memoizes.computeIfAbsent(input, f::apply) ; } ) ;

// err

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant