public
Last active

Haskell VS Groovy

  • Download Gist
HaskellVSGroovy.groovy
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
// This is a comparison between Haskell and Groovy based on some simple problems found in the following
// Haskell Introduction:
// http://learnyouahaskell.com/starting-out#ready-set-go
 
// Ex 1. If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible
// combinations between numbers in those lists, here's what we'd do.
 
/* HASKELL */
[ x*y | x <- [2,5,10], y <- [8,10,11]]
 
/* Groovy */
{ [2,5,10].collect { x -> [8,10,11].collect { y -> x * y } }.flatten() }
 
// Ex 2. What if we wanted all possible products that are more than 50?
 
/* HASKELL */
[ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50]
 
/* Groovy */
{ [2,5,10].collect { x -> [8,10,11].collect { y -> x * y } }.flatten().findAll{ it > 50 } }
 
// Ex 3. How about a list comprehension that combines a list of adjectives and a list of nouns … for epic hilarity.
 
/* HASKELL */
[adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
 
/* Groovy */
{ nouns, adjectives -> nouns.collect { noun -> adjectives.collect { adjective -> "$noun $adjective" } }.flatten() }
 
// Ex 4. Let's write our own version of length! We'll call it length'.
 
/* HASKELL */
length' xs = sum [1 | _ <- xs]
 
/* Groovy */
length = { xs -> xs.inject { acc, _ -> acc += 1 } }
 
// Ex 5. Here's a function that takes a string and removes everything except uppercase letters from it.
 
/* HASKELL */
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
 
/* Groovy */
removeNonUppercase = { st -> st.inject { acc, c -> acc + ( ('A'..'Z').contains( c ) ? c : '' ) } }
 
// Ex 6. A list contains several lists of numbers. Let's remove all odd numbers without flattening the list.
 
/* HASKELL */
[ [ x | x <- xs, even x ] | xs <- xxs]
 
/* Groovy */
{ xxs -> xxs.inject([]) { res, xs -> res << xs.findAll { x -> x % 2 == 0 } } }
 
// Ex 7. ... let's try generating all triangles with sides equal to or smaller than 10:
 
/* HASKELL */
let triangles = [ (a,b,c) | c <- [1..10], b <- [1..10], a <- [1..10] ]
 
/* Groovy */
def triangles = { def res = []
(1..10).each{ a -> (1..10).each{ b -> (1..10).each{ c -> res << [a,b,c] } } }
res
}
 
// Ex 8. ... we'll add a condition that they all have to be right triangles.
// We'll also modify this function by taking into consideration that side b isn't larger
// than the hypothenuse and that side a isn't larger than side b.
 
/* HASKELL */
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
 
/* Groovy */
def rightTriangles = { def res = []
(1..10).each{ c -> (1..c).each{ b -> (1..b).each{ a -> if ( a**2 + b**2 == c**2 ) res << [a,b,c] } } }
res
}
 
//############### Doing some I/O ###############//
 
// Ex 9. Read file and print its contents
 
/* HASKELL */
import System.IO
 
main = do
contents <- readFile "girlfriend.txt"
putStr contents
 
/* Groovy */
new File("girlfriend.txt").eachLine{ println it }
 
 
// Ex 10. Convert a file's text to ALL CAPS text and save it in another file
 
/* HASKELL */
import System.IO
import Data.Char
main = do
contents <- readFile "girlfriend.txt"
writeFile "girlfriendcaps.txt" (map toUpper contents)
 
/* Groovy */
def input = new File("girlfriend.txt")
new File("girlfriendcaps.txt").withWriter { w -> input.eachLine{ w.writeLine it.toUpperCase() } }
 
// Ex 11. Given a text file containing TO-DO tasks, remove a line chosen by the user from the file
 
/* HASKELL */
import System.IO
import System.Directory
import Data.List
main = do
handle <- openFile "todo.txt" ReadMode
(tempName, tempHandle) <- openTempFile "." "temp"
contents <- hGetContents handle
let todoTasks = lines contents
numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks
putStrLn "These are your TO-DO items:"
putStr $ unlines numberedTasks
putStrLn "Which one do you want to delete?"
numberString <- getLine
let number = read numberString
newTodoItems = delete (todoTasks !! number) todoTasks
hPutStr tempHandle $ unlines newTodoItems
hClose handle
hClose tempHandle
removeFile "todo.txt"
renameFile tempName "todo.txt"
 
/* Groovy */
def todoTasks = []
def todoFile = new File('/tmp/todo.txt')
todoFile.eachLine { todoTasks << it }
println 'These are your TO-DO items:'
todoTasks.eachWithIndex { todo, index ->
println "${index + 1} - $todo"
}
def numberString = System.console().readLine 'Which one do you want to delete?'
todoTasks.remove( numberString.toInteger() - 1 )
todoFile.withWriter { out ->
todoTasks.each { out.writeLine it }
}
 
// Ex 12. Create a SQL database with a single table, add data to it, then print the contents of the table
// Based on http://book.realworldhaskell.org/read/using-databases.html
 
/* HASKELL */
// Using Sqlite database for convenience: http://www.sqlite.org/
:module Database.HDBC Database.HDBC.Sqlite3
conn <- connectSqlite3 "test1.db"
run conn "CREATE TABLE test (id INTEGER NOT NULL, desc VARCHAR(80))" []
run conn "INSERT INTO test (id, desc) VALUES (?, ?)" [toSql 123, toSql "DB stuff"]
commit conn
res <- quickQuery conn "SELECT * FROM test"
let stringRows = map convRow res
mapM_ putStrLn stringRows
disconnect conn
where convRow :: [SqlValue] -> String
convRow [sqlId, sqlDesc] =
show intid ++ ": " ++ desc
where intid = (fromSql sqlId)::Integer
desc = case fromSql sqlDesc of
Just x -> x
Nothing -> "NULL"
convRow x = fail $ "Unexpected result: " ++ show x
 
 
/* Groovy */
// Much more convenient to use H2 in Java/Groovy: http://www.h2database.com
import static groovy.sql.Sql.newInstance
 
def sql = newInstance 'jdbc:h2:~/testdb', 'username', 'password', 'org.h2.Driver'
sql.execute 'CREATE TABLE test (id INTEGER NOT NULL, desc VARCHAR(80))'
sql.execute 'INSERT INTO test (id, desc) VALUES (?, ?)', [123, 'DB stuff']
sql.eachRow( 'SELECT * FROM test' ) { println "${it.id}: ${it.desc}" }
 
 
//############### GUI Stuff ###############//
 
// Ex 13. Create a window with a menu-bar containing a File menu with 2 items - About.. and Quit
// At the bottom of the window, there should be a status bar displaying some help for the currently
// selected menu item.
 
/* HASKELL */
 
{- demonstrates the use of a simple menu, statusbar, and dialog -}
module Main where
 
import Graphics.UI.WX
 
main :: IO ()
main
= start hello
 
hello :: IO ()
hello
= do -- the application frame
f <- frame [text := "Hello world!", clientSize := sz 300 200]
 
-- create file menu
file <- menuPane [text := "&File"]
quit <- menuQuit file [help := "Quit the demo", on command := close f]
 
-- create Help menu
hlp <- menuHelp []
about <- menuAbout hlp [help := "About wxHaskell"]
 
-- create statusbar field
status <- statusField [text := "Welcome to wxHaskell"]
 
-- set the statusbar and menubar
set f [ statusBar := [status]
, menuBar := [file,hlp]
-- as an example, put the menu event handler for an about box on the frame.
,on (menu about) := infoDialog f "About wxHaskell" "This is a wxHaskell demo"
]
 
 
/* Groovy */
 
import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL
import static javax.swing.JOptionPane.showMessageDialog as showDialog
import static javax.swing.JOptionPane.INFORMATION_MESSAGE as INFO
 
def about = { showDialog null, "This is a Groovy demo", "About Groovy", INFO }
 
new SwingBuilder().edt {
def status
def defaultStatus = "Welcome to Groovy SwingBuilder"
frame( title:'Hello world!', size: [ 300, 200 ], show: true) {
borderLayout()
menuBar() {
menu( text: "File" ) {
menuItem( text: "About..",
actionPerformed: about,
mouseEntered: { status.text = "About Groovy" },
mouseExited: { status.text = defaultStatus } )
menuItem( text: "Quit",
actionPerformed: { dispose() },
mouseEntered: { status.text = "Quit the demo" },
mouseExited: { status.text = defaultStatus } )
}
}
panel( constraints: BL.SOUTH ) {
loweredBevelBorder( parent: true )
status = label( text: defaultStatus )
}
}
}

Made Groovy Ex 6. solution more functional

Added database access code

The Groovy database Example (Ex 12) can actually be run without downloading anything, not even the db, by adding this to the script:

@GrabConfig(systemClassLoader=true, initContextClassLoader=true)
@Grab(group="com.h2database", module="h2", version="1.3.170")

Added GUI example (Ex 13). Based on the Hello World example from WxHaskell (see '4. Documentation -> Screenshots -> Samples' section):
http://www.haskell.org/haskellwiki/WxHaskell

Example 1 in Groovy can be written like this:
[[2,5,10],[8,10,11]].combinations().collect { it[0]*it[1] }
or
[[2,5,10],[8,10,11]].combinations().collect { x,y->x*y }

Same for noun/adjective:
{ adjectives, nouns -> [adjectives, nouns].combinations().collect { it.join ' ' } }

Example 10 in Haskell can be written like this:

import System.IO 
import Data.Char
import Control.Applicative

main = map toUpper <$> readFile "out.txt" >>= writeFile "outcaps.txt"

I wrote groovy-stream to give easier access to comprehension like lazy sequences. Here's Ex 1-8 using it (skipped 4 as stream wouldn't help)

// Groovy-stream versions of https://gist.github.com/renatoathaydes/5078535

@Grab( 'com.bloidonia:groovy-stream:0.7.4' )
import groovy.stream.*

// Ex 1. If we have two lists, [2,5,10] and [8,10,11] and we want to get the products of all the possible
// combinations between numbers in those lists, here's what we'd do.

Stream.from( x:[2,5,10], y:[8,9,10] ).map { x * y }.collect()

// Ex 2. What if we wanted all possible products that are more than 50?

Stream.from( x:[2,5,10], y:[8,9,10] ).map { x * y }.filter { it > 50 }.collect()

// Ex 3. How about a list comprehension that combines a list of adjectives and a list of nouns … for epic hilarity.

def adjectives = [ 'perfumed', 'dry' ]
def nouns = [ 'cat', 'book', 'horse' ]
Stream.from( a:adjectives, n:nouns ).map { "$a $n" }.collect()

// Ex 5. Here's a function that takes a string and removes everything except uppercase letters from it.

def st = 'Hello World'
Stream.from( st.toList() ).filter { ('A'..'Z').contains( it ) }.join()

// Ex 6. A list contains several lists of numbers. Let's remove all odd numbers without flattening the list.

Stream.from( [ 1..3, 4..6, 7..9 ] ).map { it.findAll { !(it % 2) } }.collect()

// Ex 7. ... let's try generating all triangles with sides equal to or smaller than 10:

Stream.from( a:1..10, b:1..10, c:1..10 ).map { [ a, b, c ] }.collect()

// Ex 8. ... we'll add a condition that they all have to be right triangles.
// We'll also modify this function by taking into consideration that side b isn't larger
// than the hypothenuse and that side a isn't larger than side b.

Stream.from( c:1..10, b:1..10, a:1..10 ).filter { b <= c && a <= b }.filter { a**2 + b**2 == c**2 }.map { [ a, b, c ] }.collect()

A shorter solution for Ex. 11 in Haskell

import System.IO  
import Data.List  

main = do        
    let file = "todo.txt"
    contents <- readFile file
    let todoTasks = lines contents     
        numberedTasks = zipWith (\n line -> show n ++ " - " ++ line) [0..] todoTasks     
    mapM putStrLn $ "These are your TO-DO items:" : numberedTasks ++ ["Which one do you want to delete?"]
    numberString <- getLine     
    let number = read numberString     
        newTodoItems = delete (todoTasks !! number) todoTasks     
    writeFile file $ unlines newTodoItems

Additional Ex1. Groovy alternatives:

[[2,5,10],[8,10,11]].combinations { it[0]*it[1] }

or:

[[2,5,10],[8,10,11]].combinations { x, y -> x * y }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.