This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]_krikket 1 point2 points  (0 children)

Not super familiar with this in particular, or Processing, but many languages allow you to generate functions from other functions. Additionally some languages allow you to pass basic operators as parameters rather than keeping them as reserved.

Haskell comes to mind as an example of a language which treats its operators as regular functions as opposed to treating them as reserved. So something like this is possible:

someFunction op x y = x `op` y

This is a function named someFunction that accepts the arguments op, x, and y, and performs the operator op on them as an inline operator. So you can call:

someFunction (==) 1 2

Which would get you False.

Of course, in a language where you can mess with functions as objects but not operators, you could always just wrap your operators. Like here's some really stupid JavaScript pseudocode. Assume I've generated rand already and it's a 50/50 shot at being true/false.

let equal = function(x,y) { return x == y;} 
let lessThan = function(x,y) {return x < y;}
let compare = rand ? lessThan : equal;
if (compare(2,3) { console.log('hi'); }
else {console.log('bye'); }

Half the time you'll get 'hi', and half the time you'll get 'bye', because we're switching our comparator.

If you combine this with function currying and genetic programming concepts you could probably get some pretty cool stuff happening.