arity

Ecto query and custom MySQL function with variable arity

孤者浪人 提交于 2019-12-10 12:43:33
问题 I want to perform a query like the following one: SELECT id, name FROM mytable ORDER BY FIELD(name, 'B', 'A', 'D', 'E', 'C') FIELD is a MySQL specific function, and 'B', 'A', 'D', 'E', 'C' are values coming from a List. I tried using fragment, but it doesn't seem to allow dynamic arity known only in the runtime. Except going full-raw using Ecto.Adapters.SQL.query , is there a way to handle this using Ecto's query DSL? Edit: Here's the first, naive approach, which of course does not work: ids

JavaScript中的柯里化(Currying in JavaScript)[翻译]

霸气de小男生 提交于 2019-12-09 23:44:59
原文来自 Currying in JavaScript(https://blog.bitsrc.io/understanding-currying-in-javascript-ceb2188c339) 函数式编程是一种将函数作为参数来传递和返回的,并且没有副作用(只是返回新的值,不改变系统变量)的编程方式。所以很多语言采纳了这种编程方式,这里面JavaScript、Haskell、Clojure·、Erlang和Scala是最受欢迎的。 并且由于它能够传递和返回函数,它也带来了许多概念: 纯函数(Pure Functions) 柯里化(Currying) 高阶函数(Higher-order functions) 在这里我们要来探索的一个概念是柯里化(Currying)。 在本文中,我们将来了解柯里化是如何工作的,并且在开发过程中有何用途。 | 什么是柯里化 柯里化是把接受多个参数的函数转换成接受单一参数的函数的操作。它返回接受余下的参数而且返回结果的新函数。 它持续地返回一个新函数直到所有的参数用尽为止。这些参数全部保持“活着”的状态(通过闭包),然后当柯里化链中的最后一个函数被返回和执行时会全部被用来执行。 Currying is the process of turning a function with multiple arity into a function with

Can clojure evaluate a chain of mixed arity functions and return a partial function if needed?

拥有回忆 提交于 2019-12-07 05:04:12
问题 Suppose you have three functions of arity 1, 2 and 3 as below: (defn I [x] x) (defn K [x y] x) (defn S [x y z] (x z (y z))) Does clojure have an evaluation function or idiom for evaluating: (I K S I I) as (I (K (S (I (I))))) returning a parital function of arity 2? I am considering creating a macro that can take the simple function definitions above and expand them to multi-arity functions that can return partial results. I would not want to create the macro if there is already a built in or

Can clojure evaluate a chain of mixed arity functions and return a partial function if needed?

不问归期 提交于 2019-12-05 10:02:38
Suppose you have three functions of arity 1, 2 and 3 as below: (defn I [x] x) (defn K [x y] x) (defn S [x y z] (x z (y z))) Does clojure have an evaluation function or idiom for evaluating: (I K S I I) as (I (K (S (I (I))))) returning a parital function of arity 2? I am considering creating a macro that can take the simple function definitions above and expand them to multi-arity functions that can return partial results. I would not want to create the macro if there is already a built in or idiomatic way to accomplish this. Here is what the expanded macros would like for the above functions:

How to call a function object differently, depending on its arity (or other information known at compile time)?

瘦欲@ 提交于 2019-12-04 19:26:08
In a function template, I'd like to call a function, or function object differently, depending on its arity (how many arguments it takes). In pseudocode: if arity(f) == 1: f(x) if arity(f) == 2: f(x, y) if arity(f) == 3: f(x, y, z) How can this be done in C++? Edit To clarify the difficulty: f(x, y, z) won't compile if f only takes 2 arguments, and vice versa, f(x, y) won't compile when f needs 3 arguments. With C++11: #include <iostream> template <typename F> struct Traits; template <typename R, typename... A> struct Traits<R (A...)> { static constexpr unsigned Arity = sizeof...(A); }; void f

Haskell: Function to determine the arity of functions?

血红的双手。 提交于 2019-12-04 08:56:33
问题 Is it possible to write a function arity :: a -> Integer to determine the arity of arbitrary functions, such that > arity map 2 > arity foldr 3 > arity id 1 > arity "hello" 0 ? 回答1: It's easy with OverlappingInstances : {-# LANGUAGE FlexibleInstances, OverlappingInstances #-} class Arity f where arity :: f -> Int instance Arity x where arity _ = 0 instance Arity f => Arity ((->) a f) where arity f = 1 + arity (f undefined) Upd Found problem. You need to specify non-polymorphic type for

How to generalize program according to arity in prolog?

吃可爱长大的小学妹 提交于 2019-12-04 06:41:39
问题 I use swi prolog. I have a fact base like this consisting of facts with arity 4. attribute(a1,a2,a3,a4). data(yes,no,no,no). data(yes,no,yes,no). data(yes,yes,yes,no). data(yes,yes,yes,yes). data(no,yes,yes,yes). And my code calculate probability of Result when I call pbayes(yes,no,no,no,Result); p(ColumnName,ColumnValue,Result):- ( (ColumnName==a1->findall(ColumnValue,data(ColumnValue,_,_,_),Liste)); (ColumnName==a2->findall(ColumnValue,data(_,ColumnValue,_,_),Liste)); (ColumnName==a3-

Haskell: Function to determine the arity of functions?

半世苍凉 提交于 2019-12-03 02:34:06
Is it possible to write a function arity :: a -> Integer to determine the arity of arbitrary functions, such that > arity map 2 > arity foldr 3 > arity id 1 > arity "hello" 0 ? It's easy with OverlappingInstances : {-# LANGUAGE FlexibleInstances, OverlappingInstances #-} class Arity f where arity :: f -> Int instance Arity x where arity _ = 0 instance Arity f => Arity ((->) a f) where arity f = 1 + arity (f undefined) Upd Found problem. You need to specify non-polymorphic type for polymorphic functions: arity (foldr :: (a -> Int -> Int) -> Int -> [a] -> Int) Don't know how to solve this yet.

How to generalize program according to arity in prolog?

断了今生、忘了曾经 提交于 2019-12-02 11:43:25
I use swi prolog. I have a fact base like this consisting of facts with arity 4. attribute(a1,a2,a3,a4). data(yes,no,no,no). data(yes,no,yes,no). data(yes,yes,yes,no). data(yes,yes,yes,yes). data(no,yes,yes,yes). And my code calculate probability of Result when I call pbayes(yes,no,no,no,Result); p(ColumnName,ColumnValue,Result):- ( (ColumnName==a1->findall(ColumnValue,data(ColumnValue,_,_,_),Liste)); (ColumnName==a2->findall(ColumnValue,data(_,ColumnValue,_,_),Liste)); (ColumnName==a3->findall(ColumnValue,data(_,_,ColumnValue,_),Liste)); (ColumnName==b->findall(ColumnValue,data(_,_,_

Why is this Java method call considered ambiguous?

青春壹個敷衍的年華 提交于 2019-11-30 11:22:33
I've come across a strange error message that I believe may be incorrect. Consider the following code: public class Overloaded { public interface Supplier { int get(); } public interface Processor { String process(String s); } public static void load(Supplier s) {} public static void load(Processor p) {} public static int genuinelyAmbiguous() { return 4; } public static String genuinelyAmbiguous(String s) { return "string"; } public static int notAmbiguous() { return 4; } public static String notAmbiguous(int x, int y) { return "string"; } public static int strangelyAmbiguous() { return 4; }