Thursday, February 24, 2011

ֵFunctional Javascript

ב''ה

I found this really terrific online JavaScript tutorial called "Eloquent JavaScript."

The interactive sight lets you run the programs in a nifty little box in the browser.

What he really means by "Eloquent" is "Functional." He goes on to describe JavaScript's call back functions, describes pure functions and side affects, closure and why writing in a functional style results in more concise and beautiful code in general and often can trump writing code for the goal of efficiency alone.

Tuesday, June 8, 2010


ב''ה




This book is making my head spin with its chapter on delegates and call back functions. I hope I understood it correctly.

Delegates and call backs are a mechanism for high order functions, like in functional programming.  You can pass functions into functions. However, it does not seem that it is built into the language of C#, but the compiler rearranges the code to work, as if the function was not passed in. You can chain them together which is pretty cool, so you can have more than one function go off together.

Still trying to wrap my head around what it really is for in C#...

I can not imagine it is quite as integral to the language as in Haskel, say, or that it has all the advantages of a functional language. Still, it seems the distinction is getting narrower and narrower...

Sunday, December 27, 2009

Typing game in perl that I wrote.

ב''ה

Err... This would have to be more on the fun side and less on the functional. None the less I am having a great time learning perl and am looking forward to figuring out any functional programming aspects it might have.

#!/usr/bin/perl -w

use List::Util 'shuffle';
use Time::HiRes;

$file = "/users/foobar/perl/words_to_spell_document";
open (FH, "< $file") or die "Can't open $file for read: $!";
my @words = ;
close FH or die "Cannot close $file: $!";

$highest_score = 0;
$highest_word_points = 0;
$highest_word = "";
$equation = "";

do
{
@movewords = shuffle(@words);
@words = @movewords;
$percent = 100;
$speed = 0;
$correct = 0;
$start_time = 0;
$elapsed = 0;
$score = 0;
$total_length = 0;
$points = 0;
$highest_word_points_this = 0;
$highest_word_this = "";
$equation_this = "";
$accuracy_bonus = 1.0;

foreach $word (@words)
{
system "clear";
$current_time = time();
if ($current_time < ($start_time + 60) or $start_time == 0)
{
print "Welcome To mtyping! (q to quit)\n\n";
printf "Accuracy: %d", $percent; print "% - $correct Words Correct out of $speed\n\n";
if ($start_time == 0)
{
$elapsed = $start_time
} else
{
$elapsed = $current_time - $start_time;
}
print "Seconds Elapsed: $elapsed\n\n";
printf "Score: %d\n\n", $score;
printf "Last : %d\n\n", $points;
print "$movewords[0]";

my $start = [ Time::HiRes::gettimeofday( ) ];

$type = "";
while ($type eq "" or $type eq "\n" or $type =~ / / )
{
#print "Start Typing! (q to quite):\n";
$type = ;
}
my $score_elapsed = Time::HiRes::tv_interval( $start );

if ($start_time == 0){ $start_time = time();}

$total_length += length($type);
if ($type eq $word)
{
shift @movewords;
$speed++;
$correct++;
$percent = ($correct/$speed)*100;
$score += length($word) * (length($word) - ($score_elapsed * $accuracy_bonus));
$points = length($word) * (length($word) - ($score_elapsed * $accuracy_bonus));
if ($points > $highest_word_points_this)
{
$highest_word_points_this = $points;
$highest_word_this = $type;
$equation_this = length($word);
$equation_this = "$equation_this * ($equation_this - ($score_elapsed * $accuracy_bonus))";
}
if ($points > $highest_word_points)
{
$highest_word_points = $points;
$highest_word = $type;
$equation = length($word);
$equation = "$equation * ($equation - ($score_elapsed * $accuracy_bonus))";
}

}
elsif ($type eq "q\n")
{
exit;
}
else
{
shift @movewords;
$speed++;
$divideby = $correct;
if ($correct == 0){ $divideby++ }
$percent = ($divideby/$speed)*100;
$score -= length($word);
$points = length($word) * -1;
$accuracy_bonus = 2 - ($percent * .01)
}
}
else
{
last;
}
}
system "clear";
print "Good Game!\n\n";
printf "Accuracy: %d", $percent; print "% - $correct Words Correct out of $speed\n\n";
$total_length /= 5;
printf "Real Words Per Minute: %d\n\n",$total_length;
$bonus = 3 * (($speed *2) + $percent);
printf "Bonus: %d\n\n", $bonus;
$score += $bonus;
printf "Score: %d\n\n", $score;
printf "Last : %d\n\n", $points;
printf "Higest Word Points This Game : %d for $highest_word_this - Equation: $equation_this\n\n", $highest_word_points_this;
if ($score > $highest_score){$highest_score = $score}
printf "Higest Score This Game Session : %d\n\n", $highest_score;
printf "Higest Word Points This Game Session : %d for $highest_word - Equation: $equation\n\n", $highest_word_points;

while ($type !~ /^y$/ and $type !~ /^n$/)
{
print "Do You Want To Play Again (y or n)?\n";
chomp($type = );
}
} while ( $type !~ /^n$/)

Saturday, November 28, 2009

can has?

ב''ה

I can has foobar?

left sided function asignment

ב''ה


Perhaps I am awestruck by this feature of perl because the 4gl language that I usually develop in has nothing like this. This is the first time I am seeing this feature in any language, although I think VB might also have it...


At any rate, assigning to a function (or to what comes back from a function call) seems to be in line with treating functions as first class citizens. Stand up for their rights people. Let them vote!


substr($foostring, 5, 3) = "foobarfoobar"

Wednesday, November 18, 2009

Recursive Function - example 2

ב''ה


A Haskell recursive function I read about to duplicate the drop function:
myDrop n xs = if n <= 0 || null xs
then xs
else myDrop (n - 1) (tail xs)

ghci> :load myDrop.hs
[1 of 1] Compiling Main ( myDrop.hs, interpreted )
Ok, modules loaded: Main.
ghci> myDrop 2 "foobar"
"obar"

Again, I wrote it out for better understanding. 



The function calls would look like this:
2 foobar
1 oobar
0 obar - which gets assigned.


Or like this:
ghci> myDrop 4 "foobar"
"ar"

4 foobar
3 oobar
2 obar
1 bar
0 ar - which gets assigned.



Only the last function call returns a value because of Haskell's lazy evaluation feature. The value only needs to be evaluated when the "then xs" expression is reached.

Tuesday, November 17, 2009

(attempt grasp this weird stuff)

ב''ה


Thank you for visiting my blog. This blog is to attempt to grasp and explain functional programing and other cool programing things.


My latest attempt at understanding this stuff was going through this article:




http://www.defmacro.org/ramblings/fp.html


The pseudo-code recursive function was too hard to understand so I wrote out what each function call would come back as.

String reverse(String arg) {
    if(arg.length == 0) {
        return arg;
    }
    else {
        return reverse(arg.substring(1, arg.length)) + arg.substring(0, 1);
    }
}


If you would pass in the string "string" this is what would happen:


What would be passed in on each recursive call:

string -length 6
tring + s - length 5
ring + t - length 4
ing + r - length 3
ng + i - length 2
g + n - length 1
'' + g - length 0


In order, what would be returned:
return g
return gn
return gni
return gnir
return gnirt
return gnirts