Optional argument in lambda function

Lambda’s take the same signature as regular functions, and you can give reg a default: f = lambda X, model, reg=1e3: cost(X, model, reg=reg, sparse=np.random.rand(10,10)) What default you give it depends on what default the cost function has assigned to that same parameter. These defaults are stored on that function in the cost.__defaults__ structure, matching … Read more

What is the preferred naming convention for Func method parameters?

There are precedents for using a noun in the Framework, e.g. Enumerable.Average<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector) Enumerable.Count<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) Enumerable.GroupBy<TSource, TKey, TElement>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector) ConcurrentDictionary<TKey,TValue>.GetOrAdd(TKey key, Func<TKey, TValue> valueFactory); The noun is often an appropriate verb with an agentive suffix. In your example I would … Read more

Check if two Python functions are equal

The one thing you could test for is code object equality: >>> x = lambda x: x >>> y = lambda y: y >>> x.__code__.co_code ‘|\x00\x00S’ >>> x.__code__.co_code == y.__code__.co_code True Here the bytecode for both functions is the same. You’ll perhaps need to verify more aspects of the code objects (constants and closures spring … Read more

Can I not map/flatMap an OptionalInt?

Primitive optionals haven’t map, flatMap and filter methods by design. Moreover, according to Java8 in Action p.305 you shouldn’t use them. The justification of use primitive on streams are the performance reasons. In case of huge number of elements, boxing/unboxing overhead is significant. But this is senselessly since there is only one element in Optional. … Read more

vararg parameter in a Kotlin lambda

I’m afraid this is not possible. The following demonstrates the type of a function with vararg. A vararg parameter is represented by an Array: fun withVarargs(vararg x: String) = Unit val f: KFunction1<Array<out String>, Unit> = ::withVarargs This behavior is also suggested in the docs: Inside a function a vararg-parameter of type T is visible … Read more

How to use a method reference on a static import?

Let’s look at the relevant part of the Java Language Specification, 15.13. Method Reference Expressions. It lists the following ways to a create method reference: MethodReference: ExpressionName :: [TypeArguments] Identifier ReferenceType :: [TypeArguments] Identifier Primary :: [TypeArguments] Identifier super :: [TypeArguments] Identifier TypeName . super :: [TypeArguments] Identifier ClassType :: [TypeArguments] new ArrayType :: new … Read more

Java 8 Pattern Matching?

I suppose you are not talking about pattern matching in the sense of applying a regular expression on a string, but as applied in Haskell. For instance using wildcards: head (x:_) = x tail (_:xs) = xs Java 8 will not support that natively, with Lambda expression there are, however, ways to do so, like … Read more