John Benediktsson: Colored Timestamps
I noticed a fun post in early December that implements a mapping between current time and a "unique" RGBA color. I thought it might be fun to use Factor to implement a colored clock. The basic concept is to map the 4,294,967,296 unique RGBA colors to seconds, which gives just over 136 years of unique colors. timestamp>rgbaWe calculate timestamps as an offset from Dennis Ritchie's birthday: : start-date ( -- timestamp ) The offset is an elapsed number of seconds from the start date: : elapsed ( timestamp -- seconds ) The conversion from a timestamp into a unique RGBA color does successive divmod operations to map into Red, Green, Blue, and Alpha values: : timestamp>rgba ( timestamp -- color/f ) You can try it for yourself, showing how the values change over time: IN: scratchpad start-date timestamp>rgba . <rgba-clock>Let's use the : update-colors ( color label -- ) Use the gadget. word to try it in your listener, and watch it update: IN: scratchpad <rgba-clock> gadget. The code for this is on my Github. John Benediktsson: Friday the 13th
In honor of January 13, 2012, a Friday the 13th, I thought it might be fun to use Factor to explore similar dates in past and future history. According to Wikipedia, such a day "occurs at least once, but at most three times a year". friday-13th?A day is "Friday the 13th" if it is both (a) Friday and (b) the 13th: : friday-13th? ( timestamp -- ? ) Trying it for today and tomorrow, to make sure it works: IN: scratchpad now friday-13th? . friday-13thsGetting all Friday the 13th's for a given year: : friday-13ths ( year -- seq ) Or, for a range of years: : all-friday-13ths ( start-year end-year -- seq ) Trying it for 2012: IN: scratchpad 2012 friday-13ths . next-friday-13thWe can iterate, looking for the next Friday the 13th: : next-friday-13th ( timestamp -- date ) Trying it for today, shows the next Friday the 13th is April, 13, 2012: IN: scratchpad now next-friday-13th . The code (and some tests) for this is on my Github. John Benediktsson: Duplicate Files
A few months ago, Jon Cooper wrote a duplicate file checker in Go and Ruby. Below, I contribute a simple version in Factor that runs faster than both Go and Ruby solutions. In the spirit of the original article, I have separated the logic into steps. Argument ParsingThe command-line vocabulary gives us the arguments passed to the script. We check for the verbose flag and the root directory to traverse: : arg? ( name args -- args' ? ) Filesystem TraversalWe can traverse the filesystem with the each-file word (choosing breadth-first instead of depth-first). In our case, we want to collect these files into a map of all paths that share a common filename: : collect-files ( path -- assoc ) Our duplicate files are those files that share a common filename: : duplicate-files ( path -- dupes ) MD5 Hashing FilesUsing the checksums.md5 vocabulary, it is quite simple: : md5-file ( path -- string ) Printing ResultsIf verbose is selected, then we print each filename and the MD5 checksum for each full path: : print-md5 ( name paths -- ) We put this all together by calculating the possible duplicate files, optionally printing verbose MD5 checksums, and then print the total number of duplicates detected: : run-dupe ( -- ) PerformanceI tested performance using two directory trees, one with over 500 files and another with almost 36,000 files. While the original article focuses more on syntax than speed, it is nice to see that the Factor solution is faster than the Go and Ruby versions.
The code for this is on my Github. John Benediktsson: Picomath
The Picomath project holds some reusable math functions inspired by John D. Cook's Stand-alone code for numerical computing, including:
These functions are implemented in an impressive list of languages: Ada, C++, C#, D, Erlang, Go, Haskell, Java, Javascript, Lua, Pascal, Perl, PHP, Python (2.x and 3.x), Ruby, Scheme, and Tcl. And now Factor! You can find the code (and a bunch of tests) for this on my Github. John Benediktsson: Slot Machines
Playing slot machines can be pretty fun, but don't be fooled by claims that the casino has the "loosest slots", odds are probably still against you. I thought it would be fun (and cheaper!) to build a slot machine simulator using Factor. Our slot machine is going to be a console application that will look something like this: ![]() SpinningEven though our slot machine is text-only, we can still make use of some nice unicode characters to be our symbols: CONSTANT: SYMBOLS "☀☁☂☃" Each spin chooses a different symbol at random (each being equally likely): : spin ( value -- value' ) To reproduce the feel of spinning a slot machine, we will introduce a slight delay so that the wheel spins fast at the beginning and then slower and slower until it stops on a symbol: : spin-delay ( n -- ) Spinning the slot machine takes a spin number, delays for a bit, then rotates each wheel (we stop spinning the first column after 10 spins, the second after 15, and the last after 20): : spin-slots ( a b c n -- a b c ) DisplayEach "spin" of the slot machine will be printed out. Using ANSI escape sequences, we move the cursor to the top left ("0,0") of the screen and then issue a clear screen instruction. Then we print out the current display and flush the output to screen: : print-spin ( a b c -- a b c ) PlayingThe player will have won if, after all the spins, the "pay line" shows three of the same characters: : winner? ( a b c -- ) Playing the slot machine consists of spinning the wheels 20 times, displaying each spin to the user, then checking if the user has won the game. : play-slots ( -- ) Since our casino wants the user to keep playing, we make it really easy to just hit ENTER to continue: : continue? ( -- ? ) And, to finish it off, we define a "MAIN" entry point that will be run when the script is executed: : main-slots ( -- ) The code for this is on my Github. John Benediktsson: Elementology
Question: What do the words bamboo, crunchy, finance, genius, and tenacious have in common? I'll give you a hint: its the same thing they have in common with the words who, what, when, where, and how? Stumped? Well, it's not that these are all English words. Answer: All of these words can be spelled using elements from the periodic table! I was recently inspired by the Periodic GeNiUS T-shirt from ThinkGeek and a website that can "make any words out of elements in the periodic table". I thought it would be fun to use Factor to see how many other words can be spelled using the symbols for chemical elements. ![]() First, we need a list of elements: : elements ( -- assoc ) Next, a word that checks if a particular substring is the symbol of an element: : element? ( from to word -- ? ) We know that symbols are only ever one, two, or three characters. A word is considered "periodic" if it can be composed of any number of (possibly repeating) element symbols. We build a recursive solution that starts with the first character and continues as long as element symbols are a match or until the end of the word is reached: : (periodic?) ( word from -- ? ) It's easy to get a list of dictionary words from most Unix systems: : dict-words ( -- words ) And then a list of all "periodic words": : periodic-words ( -- words ) So, how many words are "periodic words"? About 13.7% of them. IN: scratchpad dict-words length . The code for this is on my Github. Chris Double: Pattern Matching Against Linear Objects in ATS
In a project I’m working on I’m using linear lists. This is the Pattern MatchingWhen pattern matching against linear objects you can do a destructive match or a non-destructive match. The former will destroy and free the memory allocated for the object automatically. The latter will not. Destructive matches are done by having the pattern match clause prefixed with a
Things get complicated when doing non-destructive matches. The following won’t typecheck:
The problem with this example is that when the match is made we are effectively taking the linear object out of the variable
You’ll notice with this version that the match for In this way the linear object is never taken out, we only access it via its pointer. The Filtering a linear listIn my project I needed to filter a linear list. Unfortunately ATS doesn’t have a filter implementation in the standard prelude for linear lists (it does for persistent lists). My first attempt at writing a
This should look familiar since it’s very similar to the
One issue with this implementation is it is not tail recursive. It has stack growth proportional to the size of the result list. Tail Recursive FilteringIn Lisp code I’d often build the result list tail recursively by passing an accumulator, with each new element in the result being prepended to the accumulator. This builds a list in the reverse order so before returning it the list would be reversed. The ATS code for this is:
The Unfortunately the requirement to use One downside to this approach is we iterate over the list twice. Once to build the result, and once over the result to reverse it. Single Pass Tail Recursive FilteringThe creation of the result list can be done in a single pass if we could create a
This creates a To get a pointer to the ‘hole’ we have to pattern match:
In this example the Now that we can get pointers to the tail of the list we can implement a single pass tail recursive filter function:
The MiscellaneousThe code examples in this post use
This defines the type of the result as having a length equal to or less than that of the original list. This helps prevent errors in the implementation of the filter - it can’t accidentally leave extra items in the list. I cover this type of thing in my post on dependent types. Another addition to safety that adding the extra sorts can provide is the ability to check that the function terminates. This can be done by adding a termination metric to the function definition:
The compiler checks that A description of how It’s the usage of linear types and dealing with their restrictions that makes my examples a bit more complex. If you use ATS mainly with non-linear types and link with the garbage collector then it becomes very much like using any other functional programming language, but with additional features in the type system. My interest has been around avoiding using a garbage collector and having the compiler give errors when memory is not allocated or free’d correctly. Don’t be put off from using ATS by these complex examples if you’re fine with using garbage collection and non-linear datatypes. You might never need to deal with the cases that bring in the extra complexity. Samuel Tardieu: Accessing serial ports the easy way
Every once in a while, I see people having a hard time accessing a RS232 or USB serial port from Java. There exist several solutions to do this in Java:
However, there exist at least one other solution which does not require the use of any external library. This is what I chose to interface a Scala program with a XBee Pro module through a serial interface to interact with my students devices. I also use it to interface the Factor programming language with the same XBee module. Every language has a well-defined and well-maintained sockets library, right? So why not simply use socat, a multipurpose relay which is able to bridge many protocols and interfaces such as, in our case, TCP and a serial port? I launch socat as
and what I immediately get is a TCP server listening onto port 4161 and ready to relay any incoming connection to the But what if I want to spy on the TCP/serial relay to see that I send the right codes to the XBee module? socat offers you a choice of command-line options to dump the data in various formats. What does the Scala interface look like? I have a
socat makes my life easy. It is probably already packaged for your operating system, go and get it! Oh, and did I mention that it works with IPv6 too? (Tags: Factor, Free Software, Java, IPv6, Scala) |
Blogroll
planet-factor is an Atom/RSS aggregator that collects the contents of Factor-related blogs. It is inspired by Planet Lisp. |