background preloader

Functional programming

Facebook Twitter

How can I "store" an operator inside a variable in Perl. Delightful Coding in Scala - Using Scala Code from Java. In this article we present several program samples combining Scala code with Java code.

Delightful Coding in Scala - Using Scala Code from Java

Companion objects A companion module (or companion object) of a Scala class is an object which has the same name as the class and is defined in the same scope and compilation unit. Conversely, the class is called the companion class of the module (see SLS 5.4). // File companion.scalaclass Companion { def hello() { println("Hello (class)") } // [1]}object Companion { def hallo() { println("Hallo (object)") } // [2] def hello() { println("Hello (object)") } // [3]} Calling a Scala method from Java is straightforward (cases [1] and [2]) unless it is defined both in a class and in its companion object (case [3]): // File TestCompanion.javapublic class TestCompanion { public static void main(String[] args) { new Companion().hello(); // [1] Companion.hallo(); // [2] (static) Companion$.MODULE$.hello(); // [3] (hidden static) }} Member acessors // File TestAccessor.javaimport static example.Accessor.

Traits. Level Programming in Scala. This series is intended as a guided tour of some type-level programming I have done in Scala.

Level Programming in Scala

It generally consists of code and examples with a few lines of explanation. It is usually assumed that the reader understands the features of Scala’s type system. This is not always a good assumption about either the author or the reader of course, so comments and questions are welcome. The series is in 10 parts, with some parts consisting of more than one post each. The present post will serve as a table of contents linking to the rest of the series, so bookmark this page for easy reference. Prior work/reference: All source code is available at Get the source and use sbt console to try things out. Scala’s type system in 2.8 allows recursion, and infinite recursion at that, as demonstrated in [1,2]. To use recursion, define a trait with a type declaration. Infinite Loop Example: Purely functional recursive types in Haskell and Python. """This post is simultaneously Python and literate Haskell.

Purely functional recursive types in Haskell and Python

There is a certain truth to Greenspun's tenth law of programming. A Python project I was developing at work has slowly mutated into a compiler for a programming language without me planning it that way. Usually (I assume) compilers parse their input and construct an AST which is passed to the compiler proper. My code didn't have an AST, just a bunch of lambdas. I realised that I'd actually come across a real world example of what Wadler was talking about in Recursive Types for Free!. In Haskell, the foldr function reduces a list using a binary function and some initial value. > x = foldr (+) 0 [1,2,3] The interesting thing is that anything you might want to know about a (finite) list can be extracted using foldr.

Rich Dougherty's blog: Tail calls, @tailrec and trampolines. Recursion is an essential part of functional programming.

Rich Dougherty's blog: Tail calls, @tailrec and trampolines

But if each call allocates a stack frame, then too much recursion will overflow the stack. Most functional programming languages solve this problem by eliminating stack frames through a process called tail-call optimisation. Sententia cdsmithus. Monads Are Not Metaphors. 27 Dec 2010 This article is also available in Japanese.

Monads Are Not Metaphors

I am about to break a promise. Almost three years ago, I promised myself that I would never write an article about monads. There are too many such articles already; so many, in fact, that people are often confused by the sheer proliferation. Everyone seems to have a different take on the subject, meaning that those attempting to learn the concept for the first time are stuck trying to reason out the commonalities between burritos, space suits, elephants and desert Bedouins. I’m not going to add to this menagerie of confusing analogies. Math (or not) Here’s the thing about monads which is hard to grasp: monads are a pattern, not a specific type.

Just as a quick Ruby refresher, we can rewrite this code in the following way: Ruby has this neat convention (which is shared by most modern languages) which causes the final expression in a method to be turned into the implicit return statement. Still with me? You Could Have Invented Monads! Commercial Users of Functional Programming.

With C++

Mozart. Scala. F# Haskell. Erlang Programming Language.