commutativity

Native implementation of reduceRight in JavaScript is wrong

狂风中的少年 提交于 2019-12-06 07:35:45
For an associative operation f over the elements of array a , the following relation should hold true: a.reduce(f) should be equivalent to a.reduceRight(f) . Indeed, it does hold true for operations that are both associative and commutative. For example: var a = [1,2,3,4,5,6,7,8,9,0]; alert(a.reduce(add) === a.reduceRight(add)); function add(a, b) { return a + b; } However it doesn't hold true for operations that are associative but not commutative. For example: var a = [[1,2],[3,4],[5,6],[7,8],[9,0]]; alert(equals(a.reduce(concat), a.reduceRight(concat))); function concat(a, b) { return a

How can I specify that two operations commute in a typeclass?

此生再无相见时 提交于 2019-12-05 19:39:17
问题 I started reading this paper on CRDTs, which is a way of sharing modifiable data concurrently by ensuring that the operations that modify the data are commutative. It seemed to me that this would be a good candidate for abstraction in Haskell - provide a typeclass for CRDTs that specifies a datatype and operations that commute on that type, then work on making library to actually share the updates between concurrent processes. What I can't figure is how to phrase the contract that operations

Alternative to express “Commutativity” in Prolog?

牧云@^-^@ 提交于 2019-12-05 05:02:18
as a beginner to Prolog, I found the commutative expression in Prolog are quite not intuitive. for example if I want to express X and Y are in one family, like: family(X,Y) :- married(X,Y); relative(X,Y); father_son(X,Y). I should also add the following to the definition, in order to make it "commutative": married(Y,X); relative(Y,X); father_son(Y,X). But we use Prolog, because we want to write elegant code... so, I'd hope to add only one line(instead of the above three) to the original : family(Y,X). Here is the POINT. it leads to untermination! why is prolog no so "logical"? and is there an

Babel plugins run order

老子叫甜甜 提交于 2019-12-05 00:00:41
TL;DR: Is there a way how to specify the order in which the Babel plugins are supposed to be run? How does Babel determine this order? Is there any spec how this works apart from diving into Babel sources? I'm developing my own Babel plugin. I noticed, that when I run it, my plugin is run before other es2015 plugins. For example having code such as: const a = () => 1 and visitor such as: visitor: { ArrowFunctionExpression(path) { console.log('ArrowFunction') }, FunctionExpression(path) { console.log('Function') }, } my plugin observes ArrowFunction (and not Function). I played with the order

Argument order for '==' with Nullable<T>

和自甴很熟 提交于 2019-12-03 11:09:23
问题 The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, == . (The type of IsInitialized is bool ). Using C# 7.1 and .NET 4.7 . static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } But the IL code for the second one seems much more complex. For example, B is

Argument order for '==' with Nullable<T>

别说谁变了你拦得住时间么 提交于 2019-12-03 01:37:16
The following two C# functions differ only in swapping the left/right order of arguments to the equals operator, == . (The type of IsInitialized is bool ). Using C# 7.1 and .NET 4.7 . static void A(ISupportInitialize x) { if ((x as ISupportInitializeNotification)?.IsInitialized == true) throw null; } static void B(ISupportInitialize x) { if (true == (x as ISupportInitializeNotification)?.IsInitialized) throw null; } But the IL code for the second one seems much more complex. For example, B is: 36 bytes longer (IL code); calls additional functions including newobj and initobj ; declares four

Does boost offer make_zip_range?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 10:41:52
At this answer here on SO, there's a comment suggesting a useful C++ construct, similar to make_zip_iterator , but for ranges: It takes a tuple of ranges and produces a new range - whose begin() and end() iterators are the appropriate zip iterators. Now, this should not be too difficult to implement, but I was wondering - Isn't already offered already by Boost somehow? Boost.Range is providing combine() function as zip_iterator 's range. http://www.boost.org/doc/libs/1_56_0/libs/range/doc/html/range/reference/utilities/combine.html 来源: https://stackoverflow.com/questions/26288520/does-boost

Relax ordering constraints in monadic computation

南楼画角 提交于 2019-11-30 12:33:49
问题 here is some food for thought. When I write monadic code, the monad imposes ordering on the operations done. For example, If I write in the IO monad: do a <- doSomething b <- doSomethingElse return (a + b) I know doSomething will be executed before doSomethingElse . Now, consider the equivalent code in a language like C: return (doSomething() + doSomethingElse()); The semantics of C don't actually specify what order these two function calls will be evaluated, so the compiler is free to move

Commutative operator overloading + of 2 different objects

a 夏天 提交于 2019-11-29 10:54:50
I have 2 classes which represent a matrix: 1. RegularMatrix - O(n^2) representation 2. SparseMatrix - a matrix that is represented as linked list (without zeros). lets say i have: RegularMatrix a; SparseMatrix b; i want to be able to do: a+b; and also: b+a; so i'm overloading the + operator. My question is, since I want the addition to be commutative (a+b = b+a), do i need to implement 2 overloadings, one for each case? RegularMatrix operator+(const RegualarMatrix &, const SparseMatrix &); RegularMatrix operator+(const SparseMatrix & ,const RegualarMatrix &); or is there a general form which

3 Equals or Case Equality operator

只谈情不闲聊 提交于 2019-11-28 10:46:48
In Ruby Integer === 5 returns true . Similarly String === "karthik" returns true . However, 5 === Integer returns false . And "karthik" === String . Why is the operator not commutative? The simple answer is: because it doesn't make sense. The relationship the operator describes is not commutative, why should the operator be? Just look at your own examples: 5 is an Integer . But is Integer a 5 ? What does that even mean ? === is the case subsumption operator , and subsumption doesn't commute. The fact that the case subsumption operator uses equals signs, and that it is usually called the triple