<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Cratylus's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 13:10:31 GMT</pubDate>
    <description>DZone Snippets: Cratylus's Code Snippets</description>
    <item>
      <title>How to parse a little language in Prolog</title>
      <link>http://snippets.dzone.com/posts/show/3851</link>
      <description>How to parse a token list into functors (structured terms) in Prolog.&lt;br /&gt;The book "Logic in Prolog" by Gibbins has some good example code  &lt;br /&gt;See http://www.ddj.com/184404172, listing 9 for the little language&lt;br /&gt;&lt;code&gt;&lt;br /&gt;%&lt;br /&gt;% This is a simple (nay,trivial!)  "dialect" &lt;br /&gt;% with only two commands.&lt;br /&gt; &lt;br /&gt;% Obviously only scratches the surface - &lt;br /&gt;% Written as a learning exercise!&lt;br /&gt;% First define a grammar using prolog's Definite &lt;br /&gt;% Clause Grammar (DCG) notation&lt;br /&gt;% DCG is a bit like a macro system - &lt;br /&gt;% the grammar rules are expanded into&lt;br /&gt;% ordinary prolog clauses  before execution:&lt;br /&gt;% Thanks to prolog unification, the Cmd variable&lt;br /&gt;% will end up being instantiated to a functor like&lt;br /&gt;% sell(abc,10,5) or buy(xyz,55):&lt;br /&gt;&lt;br /&gt;cmd(Cmd) --&gt; sell,!,amount(Amount),of,stock(Stock),at,price(Price), &lt;br /&gt;             { Cmd = sell(Stock,Amount,Price) }.&lt;br /&gt;cmd(Cmd) --&gt; buy,!,amount(Amount),of,stock(Stock), &lt;br /&gt;             {Cmd = buy(Stock,Amount)}.&lt;br /&gt;sell --&gt; [sell].&lt;br /&gt;of --&gt; [of].&lt;br /&gt;at --&gt; [at].&lt;br /&gt;buy --&gt; [buy].&lt;br /&gt;amount(Amount) --&gt; [Amount].&lt;br /&gt;stock(Stock)   --&gt; [Stock].&lt;br /&gt;price(Price)   --&gt; [Price].&lt;br /&gt;&lt;br /&gt;% mini-evaluator:&lt;br /&gt;&lt;br /&gt;eval(sell(Stock,Amount,Price)) :- &lt;br /&gt; format('Sold ~d ~a shares at $~d.~n',[Amount,Stock,Price]).&lt;br /&gt;eval(buy(Stock,Amount)) :- &lt;br /&gt; format('Bought ~d ~a shares.~n',[Amount,Stock]).&lt;br /&gt;&lt;br /&gt;% parse a statement, if it's a command, &lt;br /&gt;% evaluate it, otherwise write an error&lt;br /&gt;% (NB.  ";" is prolog's % "or".)&lt;br /&gt;&lt;br /&gt;interp(Statement) :- cmd(Cmd,Statement,[]),&lt;br /&gt;             eval(Cmd);write('Unrecognised command!').&lt;br /&gt;&lt;br /&gt;% Examples: &lt;br /&gt;% ( First two match , the last fails.)&lt;br /&gt;&lt;br /&gt;test :-&lt;br /&gt;    interp([sell,100,of,xyx,at,50]),&lt;br /&gt;    interp([buy,45,of,abc]),&lt;br /&gt;    interp([not,accepted]).&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 22 Apr 2007 01:44:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3851</guid>
      <author>cratylus (crat)</author>
    </item>
    <item>
      <title>genetic algorithm in J</title>
      <link>http://snippets.dzone.com/posts/show/2834</link>
      <description>&lt;code&gt;&lt;br /&gt;-------------  Beginning of class  pga.ijs   -------------------&lt;br /&gt;coclass'pga'&lt;br /&gt;create=:3 : 0&lt;br /&gt;genes=: ? 4 $ 100  NB. 4 random integers 0-99&lt;br /&gt;)&lt;br /&gt;getgenes =: 3 : 'genes'&lt;br /&gt;setgenes =: 3 : 'genes=:y.'&lt;br /&gt;matewith=: 3 : 0&lt;br /&gt;other=. y.&lt;br /&gt;mine=. ((#genes) % 2) ? (#genes) NB. crossover&lt;br /&gt;childgene=. getgenes__other '' NB. copy others intially&lt;br /&gt;child=. conew 'pga'&lt;br /&gt;setgenes__child ((mine { genes) (mine }) childgene)&lt;br /&gt;child&lt;br /&gt;)&lt;br /&gt;perform =: 3 : 0 NB. dummy problem&lt;br /&gt;+/ genes&lt;br /&gt;)&lt;br /&gt;destroy=:codestroy&lt;br /&gt;-------------  End of of class  pga.ijs   ----------------------&lt;br /&gt;-------------  beginning of script ga.ijs ----------------------&lt;br /&gt;NB. genetic algorithm &lt;br /&gt;NB. needs pga class&lt;br /&gt;NB. define a dummy target to measure fitness against&lt;br /&gt;targetvalue =:  500  &lt;br /&gt;fitness=: 3 : 0&lt;br /&gt;object=. y.&lt;br /&gt;1000 * %(targetvalue - perform__object '')&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;load jpath '~user\classes\pga.ijs'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;top =: 4 : '(i.y.) { \: (fitness each x.)'  NB. pop top 10 returns the 10 fittest&lt;br /&gt;&lt;br /&gt;pair=: 3 : 0  NB. pick two random ga objects, mate them, and return the child&lt;br /&gt;pop=. y.&lt;br /&gt;mom=. &gt; (?#pop) { pop NB. must unbox!&lt;br /&gt;dad=. &gt; (?#pop) { pop NB.  # is length of boxed list&lt;br /&gt;matewith__mom dad&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;NB.  pop evolve generations returns the fittest ga after &lt;br /&gt;NB. sifting the top 25 ga's&lt;br /&gt;evolve=: 4 : 0&lt;br /&gt;pop=. x.&lt;br /&gt;n=. y.&lt;br /&gt;for_k. i.n do.&lt;br /&gt;kpop=.  3 : 'pop' NB. not sure if necessary to do here&lt;br /&gt;newgen=.  (pair@kpop) each i.100  NB. create 100 children&lt;br /&gt;best=.  (newgen top 25) { newgen NB. find the indices of the best, then select the corresponding objects&lt;br /&gt;pop=. best,best,best,best  NB. not really necessary?&lt;br /&gt;end.&lt;br /&gt;&gt;0{best  NB. return best&lt;br /&gt;)&lt;br /&gt;NB. usage  &lt;br /&gt;load jpath '~user\ga.ijs'&lt;br /&gt;average =: +/%#&lt;br /&gt;pop=: conew&amp;'pga' each i.100   NB. create 100 boxed ga objects&lt;br /&gt;fitness each pop  NB. should show a boxed list of fitness values&lt;br /&gt;average ; fitness each pop  NB. shows average fitness&lt;br /&gt;&lt;br /&gt;fred=: pop evolve 100&lt;br /&gt;fitness fred&lt;br /&gt;uberfred=: pop evolve 1000&lt;br /&gt;&lt;br /&gt;NB. the fitness values increase pretty slowly which is disappointing&lt;br /&gt;NB. probably needs some mutation :)&lt;br /&gt;-------------    end of script ga.ijs     ----------------------&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 15 Oct 2006 17:33:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2834</guid>
      <author>cratylus (crat)</author>
    </item>
    <item>
      <title>each with bind</title>
      <link>http://snippets.dzone.com/posts/show/2742</link>
      <description>using bind again ..&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;here's a version of a ruby/smalltalk-like each in rebol:&lt;br /&gt;&lt;br /&gt;in smalltalk you can write:&lt;br /&gt;&lt;br /&gt;collectionObject each: [:thing | thing printName ]&lt;br /&gt;ruby used the same idea etc&lt;br /&gt;- a  code block is passed to an object&lt;br /&gt;&lt;br /&gt;here's a rebol version&lt;br /&gt;&lt;br /&gt;collection: make object! [&lt;br /&gt; data: copy []&lt;br /&gt;&lt;br /&gt; each: func [blk [block!] &lt;br /&gt;             /local iterator statement ][&lt;br /&gt;             iterator:  to word! first blk&lt;br /&gt;             statement:  copy skip blk 1&lt;br /&gt;             foreach item data [&lt;br /&gt;                set iterator item&lt;br /&gt;                do bind statement iterator&lt;br /&gt;             ]&lt;br /&gt;        ]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;This works by taking an input block of the form :&lt;br /&gt;  [ :x   print x ]&lt;br /&gt;or&lt;br /&gt;[ :num  add total num ] etc&lt;br /&gt;&lt;br /&gt;iterator: to word! first blk&lt;br /&gt;just sets up iterator to be a work like a or x ( not a get-word like :a or :x )&lt;br /&gt;&lt;br /&gt;I use a get-word just as a smalltalkish reminder that this is a&lt;br /&gt;dummy variable ( [ a print a] would work too)&lt;br /&gt;&lt;br /&gt;statement: copy skip blk 1 &lt;br /&gt;just makes a copy of the rest of input block&lt;br /&gt;ie statement would be [print x] say , i suppose chopping off the first would work too..&lt;br /&gt;&lt;br /&gt;within the loop we set the value of this word ( say x) to &lt;br /&gt;successive items  in the internal datablock, so the word we indirectly talking about has a different value each time round.&lt;br /&gt;&lt;br /&gt;do bind statement iterator&lt;br /&gt;&lt;br /&gt;interprets the statement using the current value of the word and do executes it.&lt;br /&gt;&lt;br /&gt;people: make collection [&lt;br /&gt; data: ["fred" "george" "mary" "jane"]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;people/each [:person print rejoin ["Hello " person]]&lt;br /&gt;Hello fred&lt;br /&gt;Hello george&lt;br /&gt;Hello mary&lt;br /&gt;Hello jane&lt;br /&gt;&gt;&gt;&lt;br /&gt;&lt;br /&gt;I guess there might be probs if the "dummy" variable was&lt;br /&gt;also a word used internally by the object containing the each method .&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 30 Sep 2006 15:47:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2742</guid>
      <author>cratylus (crat)</author>
    </item>
    <item>
      <title>script an object using bind</title>
      <link>http://snippets.dzone.com/posts/show/2741</link>
      <description>Suppose you have a robot object with methods like&lt;br /&gt;move , attack etc. You can store a "script" in a block to automate the object&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rebol []&lt;br /&gt;&lt;br /&gt;robot: make object! [&lt;br /&gt;move: func [amount] [...] ; whatever&lt;br /&gt;turn: func [angle][..] &lt;br /&gt;fire: func [] [...]&lt;br /&gt;run: func [code][&lt;br /&gt;                   do bind code 'self&lt;br /&gt;]&lt;br /&gt;]&lt;br /&gt;&lt;br /&gt;script: [&lt;br /&gt;           move 100&lt;br /&gt;           turn 45&lt;br /&gt;           fire&lt;br /&gt;           turn 23&lt;br /&gt;           move 34&lt;br /&gt;]&lt;br /&gt;; etc&lt;br /&gt;&lt;br /&gt;robbie: make robot []&lt;br /&gt;&lt;br /&gt;robbie/run script&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 30 Sep 2006 15:34:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2741</guid>
      <author>cratylus (crat)</author>
    </item>
  </channel>
</rss>
