lazy-evaluation

Python: Lazy Function Evaluation in any() / all()

前提是你 提交于 2020-12-05 10:29:06
问题 Logical operators in Python are lazy. With the following definition: def func(s): print(s) return True calling the or operator >>> func('s') or func('t') 's' only evaluates the first function call, because or recognizes that the expression evaluates to True , irregardless of the return value of the second function call. and does behave analogously. However, when using any() (analogously: all() ) in the following way: >>> any([func('s'), func('t')]) 's' 't' all function calls are evaluated,

How to encode corecursion/codata in a strictly evaluated setting?

限于喜欢 提交于 2020-11-29 03:07:06
问题 Corecursion means calling oneself on data at each iteration that is greater than or equal to what one had before. Corecursion works on codata, which are recursively defined values. Unfortunately, value recursion is not possible in strictly evaluated languages. We can work with explicit thunks though: const Defer = thunk => ({get runDefer() {return thunk()}}) const app = f => x => f(x); const fibs = app(x_ => y_ => { const go = x => y => Defer(() => [x, go(y) (x + y)]); return go(x_) (y_)

How to encode corecursion/codata in a strictly evaluated setting?

无人久伴 提交于 2020-11-29 03:07:05
问题 Corecursion means calling oneself on data at each iteration that is greater than or equal to what one had before. Corecursion works on codata, which are recursively defined values. Unfortunately, value recursion is not possible in strictly evaluated languages. We can work with explicit thunks though: const Defer = thunk => ({get runDefer() {return thunk()}}) const app = f => x => f(x); const fibs = app(x_ => y_ => { const go = x => y => Defer(() => [x, go(y) (x + y)]); return go(x_) (y_)

JavaScript map & find at the same time: findMap?

心已入冬 提交于 2020-08-22 06:15:09
问题 How would you rewrite this without using a for loop? const a = [2, 5, 78, 4]; const expensiveFunction = n => 2 * n; let result; // Find the first number for (let i = 0; i < a.length; i++) { const r = expensiveFunction(a[i]); if (r > 100) { result = r; break; } } console.log(result); My naive approach: const result = a.map(expensiveFunction).find(x => x > 100); console.log(result); But this runs expensiveFunction on all the elements, which I would like to avoid. In the above case, we should

Lazy evaluation for logging in Java 8

99封情书 提交于 2020-08-19 05:59:14
问题 When you have values than are expensive to compute, a common pattern you see in logging frameworks is if (log.isDebugEnabled()) { String value = expensiveComputation(); log.debug("value: {}", value); } Since Java 8 added lambdas, it'd be nice to do: log.debug("value: {}", (Supplier<String>) this::expensiveComputation); Which almost works because the logging framework will do toString() on the parameter. The problem is toString() on Supplier is the implementation in Object . Is there a way to

Lazy evaluation for logging in Java 8

可紊 提交于 2020-08-19 05:57:29
问题 When you have values than are expensive to compute, a common pattern you see in logging frameworks is if (log.isDebugEnabled()) { String value = expensiveComputation(); log.debug("value: {}", value); } Since Java 8 added lambdas, it'd be nice to do: log.debug("value: {}", (Supplier<String>) this::expensiveComputation); Which almost works because the logging framework will do toString() on the parameter. The problem is toString() on Supplier is the implementation in Object . Is there a way to

Why Lazy<T> forces initialization during serialization?

安稳与你 提交于 2020-08-07 06:11:27
问题 When I checked the implementation of Lazy <T> class, I saw this block: [OnSerializing] private void OnSerializing(StreamingContext context) { T obj = this.Value; } As you can see it's forcing initialization during serialization. Does anyone know why this behavior is preferred as default? 回答1: if it doesnt and you provided lambda for initialization, where do you think it retrives the value on deserializing? lambdas are not serializable. 回答2: The whole point of a Lazy<T> is to delay evaluation

Lazy Property Initialization in static class C#

天涯浪子 提交于 2020-07-21 07:44:30
问题 I have been given this code public static class Logger { public static Func<ILogger> LoggerFactory; private static readonly Lazy<ILogger> _log = new Lazy<ILogger>(LoggerFactory); public static ILogger Instance { get { return _log.Value; } public static ILogger ConfigureLogging(string AppName, Version AppVersion) { // stuff } } } This static class is used in the application: Logger.LoggerFactory = () => Logger.ConfigureLogging(AppName, AppVersion); Logger.Instance.Information("Starting

Lazy Property Initialization in static class C#

喜欢而已 提交于 2020-07-21 07:41:08
问题 I have been given this code public static class Logger { public static Func<ILogger> LoggerFactory; private static readonly Lazy<ILogger> _log = new Lazy<ILogger>(LoggerFactory); public static ILogger Instance { get { return _log.Value; } public static ILogger ConfigureLogging(string AppName, Version AppVersion) { // stuff } } } This static class is used in the application: Logger.LoggerFactory = () => Logger.ConfigureLogging(AppName, AppVersion); Logger.Instance.Information("Starting