loicdescotte
9/24/2013 - 8:47 PM

essai.golo.coffee

# autre solution : faire une augmentation

struct tmp = { value }

augment java.util.LinkedList {
  function sum = |this| {
		let s = tmp(0)
			this:each(|item|{ 
				s:value(s:value()+item)
			})
			return s:value()
	} 		
}

# ainsi :

  println(
		list[1,2,3,4,5]:sum()
	)

# et ça peut même s'intégrer en standard à Golo
module x

#Mais bof

struct tmp = { value }

function main = |args| {

  let sum = |lst| {
		let s = tmp(0)
		lst:each(|item|{ 
			s:value(s:value()+item)
		})
		return s:value()
	} 

	println(sum(list[1,2,3,4,5]))

}
println("Hello :)")

println(List(1,2,3)(2))

val l = List(1,2,"3")

var s = Set("lions","tigers","bears") //You have to use var
println(s) //Set(lions, tigers, bears)

s = s + "Honey badger"

println(s)

s = s - "lions"
println(s)

val m = Map(
  	"firstName" -> "Bob"
	,	"lastName" -> "Morane"
)

println(
	m("firstName") + " " + m("lastName")
)

val hobbits = List("frodo","samwise","pippin")
hobbits.foreach(hobbit=>println(hobbit))

println(hobbits.head)
println(hobbits.tail)
println(hobbits.last)

println(
	hobbits.filter(hobbit => hobbit.size == 5)
)

println(
	hobbits.map(hobbit => hobbit.size)
)

println(3 match {
  case 1 => "one"
  case 2 => "two"
  case _ => "many"
})
println("Hello :)")

println(list[1,2,3]:get(2))

let l = list[1,2,"3"]

let s = set["lions","tigers","bears"] # you can use let 
println(s) #[lions, tigers, bears]

s:add("Honey badger")
#s = s + "Honey badger" -> cast to String
println(s)

s:remove("lions")
println(s)

let m = map[
	  ["firstName", "Bob"]
	, ["lastName", "Morane"]
]

println(
	m:get("firstName") + " " + m:get("lastName")
)

let hobbits = list["frodo","samwise","pippin"]
hobbits:each(|hobbit|->println(hobbit))

println(hobbits:head())
println(hobbits:tail())
println(hobbits:getLast())

println(
	hobbits:filter(|hobbit|-> hobbit:length() is 5)
)

println(
	hobbits:map(|hobbit|-> hobbit:length())
)


let x = 5

println(match {
	when x is 1 then "one"
	when x is 2 then "two"
	when x > 2 and x < 4 then "more than 2, less than 4"
	otherwise "many"
})

# see this : http://golo-lang.org/documentation/next/#_why_no_value_from_most_control_flow_constructions

let x = 2

println(match {
	when x is 1 or x is 2 then "one or two"
	when x is 3 then "three"
	otherwise "other values"
})


let y = "pouet"

println(match {
	when y oftype Integer.class then "integer: " + y
	when y oftype Double.class then "a double"
	when y oftype String.class then "I want to say " + y
	otherwise "Mais euh ..."
})


# en attendant

let item = "foo@bar.com"

let what_it_could_be = -> match {
  when item: contains("@") then "an email?"
  when item: startsWith("+33") then "a French phone number?"
  when item: startsWith("http://") then "a website URL?"
  otherwise "I have no clue, mate!"
}

# prints "an email?"
println(what_it_could_be(item))