Projects

I am currently attempting to design a system involving:

  • abstract pattern matching with:
    • constraints
    • bindings
    • composition
    • other magic
  • state update accumulation
  • composable update batches
  • meta data
  • priority management
  • modules

These are the main features of a project idea I’ve had for a while, which has recently become rather more colourful.

I originally wanted to try to build a generic pattern matching system with a new type of regular expression for almost any data type: (lists, trees, graphs, …). Similar to clojure’s core.match.

Recently I’ve been having ideas about CSS: how terrible it is at it’s job, and how I would design a replacement.

It turns out that both of these projects have similar components and ideas have crossed between them; so much so that now the projects have merged into one!

This project touches on a few computing science areas that peek my interest: programming languages and semantics, logic programming, functional programming, and pattern matching.

I still have a lot to work out, but it should be interesting and fun :]

aepokh: For this Java thing, I want a pseudo-constructor thing to make sure...

aepokh:

For this Java thing, I want a pseudo-constructor thing to make sure that when I have an object created from the name “foo”, it’s the same object as all other objects created from that name. Here’s what I’m using right now:

import java.util.HashMap; public class Item { private static...

This is how I might go about implementing it: http://pastebin.com/v0A5KjqR

What do you think?

Here's what I did today

aepokh:

Pardon the lack of a stylesheet. The coding aspect isn’t finished either, but the basic interpreter works.

I was offered a presentation on Brainfuck for my programming languages class and I want it to be really cool.

I get to enlighten my classmates about Turing-completeness.

Just kidding. I’m going to sigh when none of them care.

I approve of the comment at the bottom of the brainfuck.js script, would definitely be very cool.

cjbrowne:

aepokh:

I don’t think I’ll ever understand how closures work internally.

Function pointers, and lots of black magic.

They should have a fairly straight forward implementation. Function data would be associated with a reference to the scope stack it’s built in. Upon procedure application, a new scope is pushed on to the function’s referenced stack for the declaration of new variables. If a function expression is evaluated within this scope, it creates function data that is associated with a reference to the current scope stack. Multiple executions of the procedure would yield multiple function data each with the same scope stack tail, just with different heads.

kr-studios:

So I just took an online programming test, and I got 60 out of 100.

Wha…

I took this three times lol. The first time I got 40, second 47, then 100! ^-^

Here’s my final JavaScript solution:

function equi(A) {
  var len = A.length;
  var index = 0;
  var leftTotal = 0;
  for (; index < len - 1; index += 1) {
    leftTotal += A[index];
  }
  index = len - 1;
  var rightTotal = 0;
  var item = A[index];
  while (index >= 0) {
    if (leftTotal === rightTotal) {
      break;
    }
    rightTotal += item;
    index -= 1;
    item = A[index];
    leftTotal -= item;
  }
  return index;
}

My first two solutions were pretty terrible, mostly because I was trying to rush without really thinking about what I was supposed to be doing.

Haskell, Sum to Zero

Problem description: Sum to Zero

You are given an array of integers. Count the numbers of ways in which the sum of 4 elements in this array results in zero.

Descriptive Haskell solution:

--  Cons a value onto the beginning
--  of every element in a list
(•) = map . (:)

--  Find the combinations of a list
combs = foldr (\y vs -> vs ++ (y • vs)) [[]]

--  Given a number k and a list vs
--  The length of vs must equal k
--  And the sum of vs must equal 0
predicate k vs = (length vs == k) && (sum vs == 0)

--  Find the combinations of a list
--  Filtered by the predicate
filtered_combs k = filter (predicate k) . combs

--  Count the number of filtered combinations
process k = length . filtered_combs k

MONOLITHIC Haskell version

cthulhu k
  = length
  . filter (\xs -> length xs == k && sum xs == 0)
  . foldr (\y vs -> vs ++ (map (y:) vs)) [[]]

I’m aware that there is likely a more efficient way of computing this – in which only those combinations that are of the correct length are computed – maybe I’ll work on that next…

Functional Thinking with Neal Ford

Execution in the Kingdom of Nouns

Great post about Java, OOP, and Functional Programming.

Clojure Slurry

Been playing with Haskell for a while, I’m now going back to Clojure :].

Came up with a function called slurry. It’s pretty much currying with splat application (use of variadic arguments).

A simple example:

; slurried map of 2 arguments
(def smap2 (slurry map 2))

; can use as normal map
(smap2 inc [1 2 3 4]) ;=> (2 3 4 5)

; can use curried
(def inc-map (smap2 inc))
(inc-map [1 2 3 4]) ;=> (2 3 4 5)

For convienience I’ve created two macros to help with the creation of slurried functions. These include slurryfn and defslurry.

slurryfn allows the curry arity to be calculated from the argument list. defslurry is a shortcut for using def with a slurryfn value. Examples follow:

; Using defslurry
(defslurry f [a b c]
  (+ a (* b c)))

; Equivalent using slurryfn
(def f
  (slurryfn [a b c]
    (+ a (* b c))))

; Equivalent using slurry
(def f
  (slurry
    (fn [a b c]
      (+ a (* b c)))
    3))
; note the explicit arity count: 3

The following is my implementation of the three functions/macros:

(defn slurry
  ([f] (slurry f 0 []))
  ([f n] (slurry f n []))
  ([f n _args]
    (fn [& n_args]
      (let [args (concat _args n_args)]
          (if (< (count args) n)
            (slurry f n args)
            (apply f args))))))

(defmacro slurryfn [args & tail]
  `(slurry
    (fn ~args ~@tail)
    ~(count args)))

(defmacro defslurry [name & tail]
  `(def ~name
    (slurryfn ~@tail)))

Resources:

We really don't know how to compute (video)

Talk by Gerald Jay Sussman

0 notes

Don't Panic: I'm not actually sure if this is possible in Java

cabbagebot:

That hasn’t happened in a while.

Basically I have a class that represents an immutable data structure in which operations create new data structures rather than mutating the current one. Anyone know if there is a way to define a function in a Java interface such that each implementer returns its…

Do you mean something like this?

With the following setup:

interface EggLaying {
    <T extends EggLaying> T layEgg();
}

class Bird implements EggLaying {
    public Bird layEgg() {
        System.out.println("Bird Layed Egg");
        return new Bird();
    }
}

class Platypus implements EggLaying {
    public Platypus layEgg() {
        System.out.println("Platypus Layed Egg");
        return new Platypus();
    }
}

Test 1:

class Program {
    public static void main(final String[] _args) {
        final EggLaying bird = new Bird();
        final EggLaying platypus = new Platypus();

        final Bird babyBird = bird.layEgg();
        final Platypus babyPlatypus = platypus.layEgg();
    }
}

Output:

Bird Layed Egg
Platypus Layed Egg

Test 2:

class Program {
    public static void main(final String[] _args) {
        final EggLaying bird = new Bird();
        final EggLaying platypus = new Platypus();

        final Platypus babyBird = bird.layEgg();
    }
}

Output:

Bird Layed Egg
Exception in thread "main" java.lang.ClassCastException: Bird cannot be cast to Platypus
at Program.main(Main.java:6)

Chris Browne: Anyone know of any languages...

cjbrowne:

liamgoodacre:

cjbrowne:

Which use the comma operator a bit more usefully than in C/C++ and allow you to do this sort of wizardry:

(foo,bar).baz(); // runs the method ‘baz()’ first on ‘foo’ then on ‘bar’, illegal if either ‘foo’ or ‘bar’ do not have a method ‘baz()’ but legal even if ‘foo’ and ‘bar’ are different types

I’m experimenting with language design and would be interested in opinions on this syntax (I can see a lot of ways in which it could go horribly wrong, and implementations would have to be careful about operator precedence, but I think as long as the comma-operator has the absolute lowest precedence of all operators, it should work fine)

I think it might make more sense to say that (foo, bar) is a sequence, and the dot operator maps the call over all the elements. This prevents the comma from being an operator, and just a delimiter for the sequence.

Have a look into Array Programming languages. Although I don’t know of any Object Oriented Array Programming Languages, here is a paper I just found about it High Level OOP

I too am experimenting with language design, although less on syntax more on semantics. I might start posting about my ideas too. I was also just thinking about array programming when I saw your post!

Hope to hear more about your experiments.

Well, this was just a specific case; in the more general case I want to treat it “like” an operator that just evaluates the expressions on either side of it (left first) and returns both evaluated expressions to the statement (unlike other operators which only return one result).  It would cause the operator to create a branch in the code path, like an if() statement does only both branches are guaranteed to be executed (left-side first).

So you could also embed it into an if statement, for some even funkier effects:

if(foo,bar) { print(“something was true”); }

expands out into:

if(foo) { print(“something was true”); } if(bar) { print(“something was true”); }

The problem is that programmers unfamiliar with this style might be confused and not realise that both expressions will be evaluated, and think that using it in an if statement like above will just be an alias for the || or && operators.  The advantage, though, is that you can do the even more wizardly:

if(baz = (foo,bar)) { print(baz.toString()); print(” evaluates to true!”); }

Which would basically be syntactic sugar for:

if(foo) { print(foo.toString()); print(” evaluates to true!”); }
if(bar) { print(bar.toString()); print(” evaluates to true!”); }

Is this a good feature or a bad one, though?  I know I would use it a lot but that probably suggests it’s a bad feature… :P

The strange thing here is that an expression is used as a control flow for statements, which may get rather unusual on the semantics side of things. The following would be roughly equivalent to your last example:

for (baz in [foo, bar]) {
  if (baz) {
    print(baz.toString());
    print(" evaluates to true!");
  }
}

But reguardless of that, it’s still interesting to think about, so continuing with the idea… You could do some weird things like combining the comma operator with a double question mark to filter based on a method call:

print((foo, bar ?? validBaz()).toString());

Or maybe:

print((zuux <- foo, bar ?? zuux.validBaz()).toString());

Expanding to:

for (zuux in [foo, bar]) {
  if (zuux.validBaz()) {
    print(zuux.toString());
  }
}

Chris Browne: Anyone know of any languages...

cjbrowne:

Which use the comma operator a bit more usefully than in C/C++ and allow you to do this sort of wizardry:

(foo,bar).baz(); // runs the method ‘baz()’ first on ‘foo’ then on ‘bar’, illegal if either ‘foo’ or ‘bar’ do not have a method ‘baz()’ but legal even if ‘foo’ and ‘bar’ are different types

I’m experimenting with language design and would be interested in opinions on this syntax (I can see a lot of ways in which it could go horribly wrong, and implementations would have to be careful about operator precedence, but I think as long as the comma-operator has the absolute lowest precedence of all operators, it should work fine)

I think it might make more sense to say that (foo, bar) is a sequence, and the dot operator maps the call over all the elements. This prevents the comma from being an operator, and just a delimiter for the sequence.

Have a look into Array Programming languages. Although I don’t know of any Object Oriented Array Programming Languages, here is a paper I just found about it High Level OOP

I too am experimenting with language design, although less on syntax more on semantics. I might start posting about my ideas too. I was also just thinking about array programming when I saw your post!

Hope to hear more about your experiments.

Monads

I now understand Haskell’s Monads; this guide helped. Booyahh.