Friday, November 4, 2011

Learning Scala : Advice of the exp... Noob

Authored by Win Myo Htet



So I have learned a bit of Scala and I am exposed a bit to some Scala community and learning material. Well, I have one idea on helping the Scala learner. I have disclosed my level of knowledge on Scala and the length of exposure to Scala here. Having said that, let me jump the Gun! Of course the official Scala site would like to tout about the available resources to learn: the mailing list, the books, blog, irc and some training. It seems quite a lot but still they are not enough.

I want to Scala IRC channel. I feel like being in Shangri-la, very tranquil and quiet with all the sage like participants. Very very occasionally, there will be some discussions of topic beyond my level. I feel uncomfortable to disturb this tranquility with my ignorant questions. I have very little luck with mailing list  before and I don't even bother. The training courses are few and far between at far away location with the Enterprise price ... Books and blogs are my source of knowledge. Let's not talk about the collection of books you can choose from to begin learning Scala. I can recommend you two books: "Programming in Scala" by Martin Odersky (the Scala language author) and "Programming Scala" by Dean Wampler and Alex Payne . I pick the later.

From reading the book and some blogs, I have this idea to help learner grasp functional programming better. It is a very innovative idea of having an exercise with solution after each chapter! You see, I have been typing in the examples from the book I am reading. Yet, I am a bit clueless on where to start using these knowledge on my own. I would like to try them out but I don't know my capabilities or limits or the proper usage. I have learned that Scala is such a powerful language so that the developer can even shoot their own foot if not use properly from the comment from this blog.

So, I think that the book author can provide some exercises at the end of each chapter for the learners to get their hands dirty with. Then, they can also provides the solution with proper comments to guide the learners to learn the proper usage patterns and thus to avoid shooting their own foot. Isn't my idea wonderful?

Anyway, let me try to get my hands dirty on my own. I have heard that it is required to code fizzbuzz in the interview. So let's give it a try.

def createInList():List[Int]=(for(i <- 1 to 100)yield i).toList

def fizzBuzz(ilist:List[Int]):List[String]
= ilist.foldRight(List[String]()){
 (x,list)=>{
  if (x%15==0) "fizzBuzz"::list
  else if(x%5==0)"Buzz"::list
  else if(x%3==0)"fizz"::list
  else x.toString ::list
 }}

val inlist= createInList
println(inlist)
println(fizzBuzz(inlist))

Here it is, so am I in? When can I start ?

Authored by Win Myo Htet

UPDATE
my friend Haskell's fizzbuzz

fizzBuzz :: Int -> [Char]
fizzBuzz x | x `mod` 15 == 0 = "FizzBuzz"
| x `mod` 3 == 0 = "Fizz"
| x `mod` 5 == 0 = "Buzz"
| otherwise = show 


Authored by Win Myo Htet

No comments:

Post a Comment