• Log in with Facebook Log in with Twitter Log In with Google      Sign In    
  • Create Account
  LongeCity
              Advocacy & Research for Unlimited Lifespans


Adverts help to support the work of this non-profit organisation. To go ad-free join as a Member.


Photo
- - - - -

Any Scala programmers


  • Please log in to reply
1 reply to this topic

#1 Connor MacLeod

  • Guest
  • 619 posts
  • 46

Posted 17 June 2011 - 02:38 AM


Just curious is anyone here uses Scala? I've been playing around with it for the past couple months, and though I have a few quibbles, so far I really like it. I haven't attempted any serious projects in it yet, but I'm just about at that point. In any case, Scala seems to do a reasonable job of bridging the object oriented and functional programming worlds; and the fact that it allows immediate access to all java libraries (it runs on the JVM and compiles to Java byte code) is a big plus for programmers who have time invested in Java.

http://www.scala-lang.org/

#2 Connor MacLeod

  • Topic Starter
  • Guest
  • 619 posts
  • 46

Posted 02 July 2011 - 08:18 PM

Here's a functional programming implementation of the sieve of Eratosthenes in Scala using Streams (a Stream is like a list but the entries are lazily evaluated, i.e. they only computed as they are needed):

def sieve(s: Stream[Int] = Stream.from(2)): Stream[Int] = Stream.cons(s.head, sieve(s.tail.filter(_ % s.head != 0)))

And to print out the first 50 primes...

sieve().take(50).foreach(p => print(p + " "))


2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229

Kinda slick!

Edited by Connor MacLeod, 02 July 2011 - 09:03 PM.


sponsored ad

  • Advert



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users