11 ms·
Hands-on Scala Programming
- lihaoyi 6y agoI didn't post this, but cool to see this up here! The full book is ~400 pages and will be released Summer 2020 (i.e. soon!). If you're interested in being notified when it comes out, there's a mailing list link on the page and I'll let you know when it's out. There will be both E-Books as well as paperbacks & hardcovers (assuming the supply-chains are still open in this COVID-19 global environment...) The first 5 chapters are already online, for free now and forever. These form a self-contained introduction to the Scala language: setup, basic syntax, collections, and a notable language features. If you're looking for a quick introduction to Scala, this is it. The rest of the book goes heavily into use cases and projects in a way that few other programming books do: interactive websites, network file synchronizers, programming language interpreters, migration tools, parallel web scrapers, and much more. The book intentionally skips a lot of language esoterica in favor of showing how to use Scala to do things someone might pay you a salary for. You will learn Scala by building a breadth of useful experience, rather than a depth of knowledge in language minutiae. The chapters in the book are all based on real projects I have worked on. Most projects take <100 lines of code (i.e ~2 printed pages), and it is a testament to the Scala language that we can build working prototypes of all these things in a single 400-page book! If this interests you, feel free to leave your email on the website, and browse through the free chapters already available.
- deleted 6y ago[deleted]
- mason55 6y agoThis is very cool, I love the idea. How does the book handle Scala 3?
- lihaoyi 6y agoIt doesn't for now, and doesn't really need to. Scala 3 isn't out yet, and when it is there will be a migration period (as always) while things will remain backwards compatible and libraries will remain cross compiled. This book also explicitly skips over all the deep language esoterica, in order to focus on use cases. As a happy consequence, I expect ~all code samples to continue working unchanged in Scala 3 for the foreseeable future.
- totalperspectiv 6y agoWill the ebook be released before the physical version? This is EXACTLY the book on Scala I've wanted and am so excited to see this coming!
- lihaoyi 6y agoI can’t really promise the “before”, but the e-book will be released when the current round of review and edits is complete. The physical books released when I can figure out the logistics of printing and distribution
- totalperspectiv 6y agoSpeaking as an Nof1, if you were to do an early access release like Manning does with their MEAP books, I would go for that.
- nafizh 6y agoWill this be a good book to learn your first functional language?
- lihaoyi 6y agoYes! We use a lot of functional techniques in this book, but we do not dwell on them. In the end you will be using functional techniques not because they’re interesting, but because they’re the obvious and easy. That is how functional programming should be!
- throwaway_pdp09 6y agoFunctional programming is a style or pattern. If a language gives you basic stuff, you can do that pattern. I learned a bit of haskell a long time ago - mind blown kind of time - then found javascript had closures (erm, kind of a way of turning functions into objects-with-state-that-still-look-like-functions) and functions as first class objects (meaning a function can be passed around exactly like and integer or a string) and so I used it in javascript. Google up "javascript functional" and see what happens. Do some exercises. Beware there are libraries out there that make simple functional Jscript stuff look entirely unnecessarily complicated. And when you're ready, pick up this guy's book! Edit: that may have come across a bit insulting towards the author. All I'm saying is you can learn the functional stuff while you wait for the book to come out. And when you have the functional stuff under your belt, get the book and you'll be one large step better prepared to dive into it - scala has a lot more to offer than just functional programming, and the book will surely cover a lot of ground.
- codygman 6y ago> Functional programming is a style or pattern. If a language gives you basic stuff, you can do that pattern. Kind of... correctness isn't as guaranteed though. Having a hard time putting it into words at the moment, maybe I just need some holes poked into it to draw out the rest of my view (or change it).
- jim-jim-jim 6y agoRegardless of how good a book may be, I think Scala itself might prove to be a challenging language to learn FP through. I use it daily at work and definitely appreciate all its strengths, but I don't think I'd ever really understand it if I hadn't studied Haskell first. I'd recommend it as a second language, after you've been bitten by the bug and want a job writing functional code.
- forty 6y agoAs someone who really like reading your blog articles, I'm super happy to see this :) I'll probably purchase it, as a part of my strategy to make my friends and colleagues start scala :) I'm looking forward to a "advanced scala programming" follow up ;)
- lihaoyi 6y agoHaha one step at a time. First I need to finish this final round of reviews and edits to get this manuscript out the door!
- christiansakai 6y agoLooks like Scala instead of OCaml is in my next language to learn.
- yashap 6y agoYou should! Been coding in Scala in my day job for the past 5 years, and it’s a great language - my personal favourite. Elegant/beautiful AND practical. The mix of OOP, FP, really robust static type system, and great 1st class concurrency support is a direction most languages are moving in, and Scala already nails it. There’s a significant learning/ramp-up period, but it’s good learning that helps your programming in any language, and once you start getting comfortable with the language you become quite productive.
- 9q9 6y agoI agree. I've used both heavily, they are both amazing, but Scala nowadays has a much richer ecosystem of libraries (because of the JVM heritage). Nevertheless, they are both in the ML tradition of languages, and once you've mastered on, the other is easily picked up.
- taikawatiti 6y agoI can vouch for lihaoyi. He is one of the more prolific people in the scala community writing top notch stuff. The best part about most of the stuff be writes is they are extremely ergonomic, even by scala standards. Look at either scala-tags or even better his parsing library. The parsing library is actialy so good you'll feel like finding more problems to solve. Its a breeeze to get started with (even in repl). Scala-tags also is amazing and has spawned other similar projects , one of which is laminar which is currently one of the top scala-js frameworks. In fact I can probably say this for a lot of his work, anyone learning from him would probably learn a user centric way of library design.
- lihaoyi 6y agoGlad you like them! The reason Scalatags is so good is that it is my 7th iteration of a HTML templating library: starting from using XHP at work, I had written my own templating libraries in PHP, Python, C#, F#, and 2 others in Scala before settling on the Scalatags style a few years ago that is basically unchanged today. Given how much work I’ve obsessively put into that effort over many years, the final library damn well better be good to use!
- lowiqengineer 6y agoHe is, and nobody even talks about how his dad is the PM of Singapore and his grandfather founded the country.
- taikawatiti 6y agoI can tell you if this were true he wouldn't be programming a language that has to deal with jvm.
- lowiqengineer 6y agoThat is what Google implies...unless they just share names, universities, and ages.
- kalium-xyz 6y ago
- surroundingbox 6y agoCould you compare your book to ``Scala for the impatient´´?,
- lihaoyi 6y agoSure! I have the table of contents for "Scala for the Impatient" open in front of me: - S4tI goes into a lot more detail into language features: custom extractors, abstract overrides, annotations, higher kinded types, custom operators. Hands-on Scala skips all of that. Most developers do not need to know this stuff to get stuff done professionally. - Hands-on Scala spends its pages working on fun use cases instead! As far as I can tell, S4tI doesn't cover use cases at all. Sure it's great you know about structural/compound/existential types, but it's setting up webserver, mangling a database, or scraping someone's website that gets you paid a salary. - S4tI is a bit out of date (at least the preview copy I have) and covers things like Delimited Continuations, XML processing, and the scala-parser-combinators and scala-actors libraries which you shouldn't really be using any more - S4tI doesn't really use third party libraries AFAIK; that means things like working with files uses the `java.io` or `scala.io` APIs, which are strictly worse than the modern alternatives. In fact, it is only due to the modern collection of libraries such as Requests-Scala, OS-Lib, Cask, Quill, etc. that allows Hands-on Scala to cover as much ground as it does in a tiny amount of code. Hands-on Scala leverages this ability to the fullest!
- surroundingbox 6y agoThanks a lot, insightful. It seems the Hands-on is very appropriate.
- bigbizisverywyz 6y agoHiya, found a typo - couldn't see where else to mention it so adding here, maybe the author will notice. Ch 3. This chapter will not covering any Scala-specific should be: This chapter will be not covering any Scala-specific Otherwise enjoying what I've read so far. I've just been dropped in at the deep end of a project with no scala experience, so I'm trying to get off the ground asap.
- bigbizisverywyz 6y agoWell that was totally wrong. should be: This chapter will not be covering any Scala-specific
- truelaurel 6y agogreat action to make scala more accessible to developers. this should be a concrete answer to https://www.lihaoyi.com/post/TheDeathofHypeWhatsNextforScala.html https://www.lihaoyi.com/post/TheDeathofHypeWhatsNextforScala...
- hackerm0nkey 6y agoI think they are both from the same author.
- truelaurel 6y agoyes haoyi is taking concrete action to move scala forward
- cmollis 6y agoawesome.. your blog has been extremely helpful to me.. looking forward to the book.
- fakecrusade 6y agofyi, he is also the son of current Singapore Prime Minister Lee Hsien Loong
- christiansakai 6y agoI work JavaScript/TypeScript day to day, but I like Python syntax and easiness but also like Go's concurrency. Having learned Haskell and Rust in the past, these days I appreciate simpler language. In what way does Scala have Python's convenience and Go's concurrency? Based on talking to a few people that used Scala in the past they said they wouldn't use it again and they said I probably wouldn't like it.
- nicoburns 6y agoIf you like simpler languages, you'll probably hate Scala. It's one of the most complex out there.
- AheadOfTime295 6y agoYMMV. Some features of Scala 2.x have been dropped in Scala 3 (ETA late 2020), others added. Features we know Scala 3 will have https://dotty.epfl.ch/docs/ https://dotty.epfl.ch/docs/ A comparison of features across Scala 2.x and Scala 3 is the migration guide https://github.com/scalacenter/scala-3-migration-guide https://github.com/scalacenter/scala-3-migration-guide
- jpgvm 6y agoOn the other hand you may like Kotlin. It has a lot of what makes Scala good with less of the bits that make it bad.
- switchbak 6y agoThis is down voted, but for someone wanting to learn a Scala like language, it delivers most of the capability with less complexity and tooling problems. I still prefer Scala, but found Kotlin to be enjoyable to write as well. Probably a perfect gateway into Scala if that's an eventual goal too.
- eismcc 6y agoAgree. It’s a bag of knives. Used scala for many years at Twitter and other places and There’s always a massive combination of tooling and code reviews that eventually hammer the devs into shape.
- petilon 6y agoThis book has a section on "Why Scala" but not on "Why Functional Programming". What is interesting about Scala, if you already know Java (and most CS grads already do), is that Scala is a functional language. Anyone know of a good book or article that gives an overview of functional programming and answers the question of "Why Functional Programming", without going into details any one functional language?
- noelwelsh 6y agoAs someone who has been in the functional programming world for a long time I don't think you can really understand it without some time using it. Beginners tend to focus on language features (immutability, first class functions, map instead of loop, etc.) but it's really a completely different approach to programming than imperative programming. A paradigm shift, in the original sense of the word. It is also difficult to talk about concepts without some notation, and a notation is exactly what a programming provides (as well as other things)! That said, several people have attempted to write about it. John Hughes' "Why Functional Programming Matters" is one that often comes up. There are a million versions of this floating around the web. Here is one: https://www.youtube.com/watch?v=bL-CcjKW1lw https://www.youtube.com/watch?v=bL-CcjKW1lw I gave a talk on this topic, with examples in Scala: https://www.youtube.com/watch?v=bL-CcjKW1lw https://www.youtube.com/watch?v=bL-CcjKW1lw This paper is about the idea of paradigm shifts in programming, and is beautiful and profound, though not about functional programming: https://www.dreamsongs.com/Files/Incommensurability.pdf https://www.dreamsongs.com/Files/Incommensurability.pdf
- spir 6y agoBtw, I find a lot about Scala to be interesting besides that it supports pure functional programming. For me, Scala is much nicer for imperative/OOP than Java, in ways that aren't just skin deep. https://medium.com/@ryanberckmans/why-i-like-scala-45e65cde3365 https://medium.com/@ryanberckmans/why-i-like-scala-45e65cde3...
- sideeffffect 6y agoI suggest you read a blog post What's Functional Programming All About?, written by the author of the book himself. https://www.lihaoyi.com/post/WhatsFunctionalProgrammingAllAbout.html https://www.lihaoyi.com/post/WhatsFunctionalProgrammingAllAb...
- dr0l3 6y agoSome context for people who have limited knowledge of Scala. Lihaoyi is one of the most prolific coder's, not just in Scala, but in any language. He looks like he is much to humble and modest to claim so himself, so I will do it for him. Here is a by no means exhaustive list of projects - A buildtool - A bash replacement that speaks Scala - A test framework - A parsing library - Ports of several python libraries in Scala - Several libraries in the scala.js ecosystem For the full list see https://github.com/lihaoyi?tab=repositories https://github.com/lihaoyi?tab=repositories Discussions on Scala on HN tends to contain a somewhat large volume of comments that explicitly or implicitly tries to communicate that Scala is a language that is much to complicated to really be productive in. Lihaoyi is a rather clear example of why this is not true. If this random internet citizens endorsement doesn't convince you maybe the book will :)
- xwolfi 6y agoSo your list of already existing everywhere projects is an argument for Scala ease ? Sorry but saying a dude on github has to do a test framework or port libraries, convince me Scala isn't ready.
- brianyu8 6y agoI think the parent is just giving background about the author of the book, who wrote all of those tools mentioned. I don't think they're only trying to convince you that Scala is easy, but maybe I'm reading it incorrectly.
- christiansakai 6y agoI think your argument here is counterproductive. Lihaoyi is a prolific coder but most coder like me are average Joe. How can Scala be useful and make average Joe like me productive. The fact that Lihaoyi is a prolific coder in any language does not help selling Scala.
- justinhj 6y agoHaoyi makes libraries that are very easy to use, modelling them after popular Python libraries. If you're interested he recently wrote a long blog about the future of Scala being exactly this approach; make easy to use libraries for people like yourself interested in being productive in Scala quickly. https://www.lihaoyi.com/post/TheDeathofHypeWhatsNextforScala.html https://www.lihaoyi.com/post/TheDeathofHypeWhatsNextforScala...
- brentonator 6y agoIn light of social distancing can we get a not-so-hands-on version?
- z3phyr 6y agoA little off topic: Are there similar hands on books for high performance systems programming in C, C++ or Rust?
- Kydlaw 6y agoThere is Rust by example [1]. Maybe it is close to what you look for? [1] https://doc.rust-lang.org/rust-by-example https://doc.rust-lang.org/rust-by-example
- sscarduzio 6y agoGreat book! Scala really needed some pragmatic programming resources! And I really like the website, is it open source?
- yingliu4203 6y agoHaoyi is an amazing guy. Last night at 8pm and 2pm this morning I submitted two PR to the Mill, he merged within one hour. In about 7 years, he helped to create an eco-system around Scala: - REPL (ammonite) - build tool (mill) - JSON serializer(uPickle) - web framework (cask) - test framework(uTest) - http client lib (requests-scala) - HTML tag lib (scalatags) - parser(fast parse) Scala development and tool can be divided into four categories - Java (OO): wrong idea for many developers. Use Kotlin if you like OO. - Haskell (pure FP, even HKT): good for fun but not for profit. Use Haskell for pure FP. - Erlang (Actors): fibers are better. Use Clojure if you like Actors. - Easy: practical FP, the above tools from Haoyi. Use them if you program in Scala I'd guess all is done after work. Please support him to make Scala better: https://www.patreon.com/join/lihaoyi https://www.patreon.com/join/lihaoyi
- codygman 6y ago> - Haskell (pure FP, even HKT): good for fun but not for profit. Use Haskell for pure FP. My day job disagrees Haskell isn't good for profit ;)
- yingliu4203 6y agoHaskell is not for every Joe working on ERP projects
- codygman 6y agoI argue after the initial learning curve every Joe would find it easier to avoid bugs in Haskell. https://www.simplehaskell.org/ https://www.simplehaskell.org/ Then if you use simple Haskell for a year and want more type safety, you can fearlessly refactor to get that.
- acjohnson55 6y agoI think you're missing perhaps the biggest camps in the form of the Lightbend ecosystem (very much pragmatic OO/FP hybrid) and the big data ecosystem around Spark. Even though the Lightbend stack also has Java APIs, the Scala APIs are very much their own thing.
- nomanlagharipq 6y agoare ok with this mod ? please visit http://ntechappspk.blogspot.com/2020/04/boo-video-status-maker-apk-mod-all.html http://ntechappspk.blogspot.com/2020/04/boo-video-status-mak...
- amdelamar 6y agoThis book looks great, and I can't wait to pick up a physical copy. Thank you lihaoyi!
- spiderjerusalem 6y agoTangential but I just found out that the author is the son of Singapore's PM Lee Hsien Loong.
- ggregoire 6y agoI guess this question pops up every time… what are some good reasons to choose Scala over Haskell or F# if you are interested in the FP part of Scala?
- Sunscratch 6y agoWhen you’re choosing Scala you have all the perks of JVM platform. You can reuse Java libraries if needed. Scala has pretty powerful type system. JVM in turn is a pretty good platform, with decades of effort, which can tolerate high load, make different optimizations in runtime, tolerate large heaps if needed, GC story in Haskell runtime is not even close to JVM.
- submeta 6y agoHonestly interested to know: Is there any added value for a Python developer in learning and using Scala? I love Python, especially because of its ecosystem, its widespreadedness, and of course because of Pandas, Numpy, Matplotlib. But I miss functional constructs from Mathematica or Scheme. Scala seems to be much more systematic and profound, but again: For everyday usage, is it overkill?
- finalfantasia 6y agoI cannot answer your question. Since you love Python, I guess you don't have any problem with dynamic type-checking. You also mentioned Scheme, I take that as you don't mind parentheses either. :) If you would like to try something that's closest to Scheme and has access to the rich libraries in both the Python and Java ecosystems, give Clojure a try. You have access to the Java libraries out of the box and, with libpython-clj [1], you get access to your familiar Python libraries. [1] https://github.com/clj-python/libpython-clj https://github.com/clj-python/libpython-clj
- justinhj 6y agoIt depends on your needs. Scala has some advantages over Python when you want high performance, or if you want to build large programs, or if you're interested in building principled or robust programs using functional programming. Netflix machine learning seem to be leaning towards using both Python and Scala together to take advantage of both. See this project: https://netflixtechblog.com/open-sourcing-polynote-an-ide-inspired-polyglot-notebook-7f929d3f447 https://netflixtechblog.com/open-sourcing-polynote-an-ide-in...
- lihaoyi 6y agoI love Python too! The style of Scala used in this book is much closer to Python than Mathematica or Scheme, with many of the libraries involved being carbon copies (os-lib, requests-scala, ujson) so you will feel right at home. As to what you get going to Scala, it depends what matters to you. Does 50x faster compute matter to you, for the code that’s CPU bottlenecked but inconvenient to rewrite in C? How about easy parallelism, to trivially and fully utilize multiple cores without clunky subprocess management? How about easy packaging? One builtin command to package your program as a single file that can run anywhere with a JVM, no other global installs required? You do give up some things using Scala: compile times are slower than iterating on Python scripts, the JVM runtime takes a second or two to warm up when launched, and you don’t have quite the same numpy/scipy machine-learning ecosystem. Whether that is worth it is up to you!
- sparklepwny 6y agoscala blows, use kotlin
- dang 6y agoPlease don't do this here.
- dionian 6y agoHaoyi's libraries (e.g. ujson) are great - and perfect for beginners to learn Scala with, very idiomatic scala without the Haskell-style overhead.
- codygman 6y agoI agree about Haoyi's libraries, but you're mention of overhead reminds me of the overhead you run into trying to be purely functional in Scala. If you're sold on benefits of purely functional programming, I think it's good to know (at least as of a few years ago) Scala makes that more painful than Haskell if you have even a little Haskell experience.
- airstrike 6y ago> Knowing the language alone isn't enough to go to production: Hands-on Scala introduces the reader to the ecosystem of editors, build tools, web frameworks, database libraries, everything necessary to do real work using Scala. You will finish this book having all the necessary building blocks to be productive using Scala in production. We definitely need more books to include this by default. Kudos! I only wish a book like this existed for F#! If anyone has any recommendations, I'm all ears
- InnerGargoyle 6y agomy wish too..
- bogomipz 6y agoBoth the content and layout looks really fantastic! I wanted to ask any experienced Scala folks if its possible become really proficient without a solid background in Java. I understand that its only JVM hosted language so I guess my concern is how much Java you need to know when things go sideways - stack traces, low level debugging etc?
- philipkglass 6y agoI learned Scala on the job with no previous Java or JVM experience. At the time I had been writing software professionally for 8 years. Before Scala I had professionally written C, Fortran, and Python. You will inevitably have to learn about the Java ecosystem when things go sideways. One example that comes to mind is problems with dependency resolution. SBT, the most common Scala build tool, allows you to add libraries to your project easily with notation like this: libraryDependencies += "com.lihaoyi" %% "upickle" % "1.0.0" Normally that is fine. But when things go awry, sbt may provide unhelpful messages. You actually end up having to learn about how Java's Maven (or Ivy) and its repositories work. A couple of instances I recall: - SBT repositories were configured to look for dependencies on site A. A was redirecting downloads to site B. Site B was configured with an expired TLS certificate. The error message reported from sbt only mentioned site A. I only figured out what was going on by tracing the network activity. - Maven central disabled insecure (HTTP) access earlier this year and started requiring HTTPS. Some of our build configurations still referenced repositories via HTTP URLs. I don't remember what the SBT error message was, but it was again cryptic and unhelpful. Other developers who mostly work with Go had to enlist my help in diagnosing the problem and fixing it. A lot of Scala teaching material also assumes familiarity with Java. "Scala's Traits are like Java Interfaces, but with these improvements: ...." That didn't help when I hadn't used Java Interfaces before. Fortunately, it looks like Hands-On Scala Programming doesn't assume a Java background. It also took me longer than it should have to understand the deep stack traces you often run into. The key: look for lines that reference your own application code, and focus there. (But this doesn't work if you are really encountering library bugs or infrastructure flakiness.) The traces can be very deep and a lot of the lines are not really relevant to solving your problem. I was accustomed to reading shallower Python traces. Learn to use a debugger early on. JVM code debugging with an IDE is powerful and easy to use. Plus you can't just monkey-patch your dependencies on the fly to add more diagnostics, the way I had done in Python.
- nullobject 6y agoThis looks great, looking forward to reading it.