#916 Fantom cookbook

fury Fri 15 Jan 2010

Have you guys seen this site: http://pleac.sourceforge.net/ ? I think such a section would be nice to have in the docs section ( I'm sorry and would be very ashamed if this already exists and I didn't find it ). Even though I'm new to Fantom, I'd love to contribute.

What do you think?

brian Sat 16 Jan 2010

Never seen that site, but seems like there are a lot of those sorts of things.

The more examples of Fantom code out there the better!

fury Sat 16 Jan 2010

I started to add stuff from PLEAC. You can find them at DZone snippets, under the tag Fantom: http://snippets.dzone.com/tag/fantom . I started with the array section, hopefully I can post many of them this weekend. If you guys have any suggestions about the code, I'd be glad to hear them.

DanielFath Sat 16 Jan 2010

Nice idea. Though I must ask, why are your code snippets made into classes? If I recall Fantom doesn't require you to make classes, you can just make scripts (unless you are required to use the default way of doing things).

Some examples like printing lists with commas are pointless since by default List.toStr already does that.

fury Sat 16 Jan 2010

About the classes ... Java habit I guess. And the printing with commas, the example in the cookbook turned a list like [1,2,3] in "1,2 , and 3".

tactics Sat 16 Jan 2010

If I recall Fantom doesn't require you to make classes, you can just make scripts

You can't run code in a vacuum. Even in a script, all code is run within a class.

What makes a Fantom script a script is that you do not need to compile it to a pod file. But the actual source must be valid Fantom source code, meaning the only top-level entities allowed are using statements, classes, mixins, and pod declarations.

DanielFath Sun 17 Jan 2010

Yeah, I know. What I meant that his classes in his snippets were unnecessary. PLEAC is a cookbook not a programming guide so omitting classes is almost recommended (at least in the simple cases).

ivan Mon 18 Jan 2010

My vision of some of code samples (modifying your examples), I believe this is more fantomy:

Custom Sorting a list

/* let's sort the list by the numbers contained in each word */
a := ["String10","String9","String1","String2","String4"]
a.sort |one, two| { one[6..-1].toInt <=> two[6..-1].toInt }
echo(a)

Sorting an array numerically

echo([4,3,2,5,7,9,21,1].sort)

Finding all elements in an array matching certain criteria

a := [1,2,3,4,5,6,7,8,9]
/* let's find all numbers that are dividable by 3 */
dividableBy3 := a.findAll |e| { e % 3 == 0}

randomStrings := ["one3943two","three9430four","five"]
r := Regex <|(\d+)|>
/* let's find all the words that also contain digits
   the array should contain "one3943two" and "three9430four" */
containingDigits := randomStrings.findAll |str| { r.matcher(str).find() }
echo(containingDigits)

Printing a list with commas

result := list[0..<-1].join(", ") + ", and $list.last"

fury Mon 18 Jan 2010

Awesome!

When I get back from work I'll edit them. I'm almost done with the arrays chapter. If anyone wants to start with one of the chapters, that would be nice as well.

mopoke Fri 22 Jan 2010

I just stumbled across Fantom for the first time today - I've been learning Scala and quite enjoyed reading about Fantom as another language promoting the functional style.

To get my hands dirty, I've been tackling some of the problems from Langref - mainly starting with Strings and Numbers.

Unfortunately langref doesn't (currently) have a section for Fantom, but if anyone's interested in seeing any of the snippets, I'd be happy to share them here or elsewhere.

Andy

fury Fri 22 Jan 2010

Why don't you put them on http://snippets.dzone.com?

mopoke Fri 22 Jan 2010

Good idea :-) All now up here: DZone

Andy

Login or Signup to reply.