-
-
Save consoledotblog/ec5f3a1cc94272e6678d to your computer and use it in GitHub Desktop.
Overcoming Immutability in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final List<String> fullNames = new ArrayList<>(); | |
for (Name name : names) { | |
fullNames.add(String.format("%s %s", name.getFirst(), name.getLast())); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val fullNames = names.map { name => | |
s"${name.first} ${name.last}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val fullNamesStartingWithM = names | |
.filter(_.last.startsWith("M")) | |
.map(name => s"${name.first} ${name.last}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<String> fullNamesStartingWithM = | |
names | |
.stream() | |
.filter(name -> name.getLast().startsWith("M")) | |
.map(name -> String.format("%s %s", name.getFirst(), name.getLast())) | |
.collect(Collectors.toList()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Foo foo; | |
if (someTest()) { | |
foo = someFoo; | |
} else { | |
foo = new Foo(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val foo = | |
if (someTest()) { | |
someFoo | |
} else { | |
new Foo | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val foo1 = | |
if (someTest()) { | |
someFoo | |
} else { | |
new Foo | |
println("Using default value for `foo`") | |
} | |
val foo2 = | |
if (someTest()) { | |
someFoo | |
} | |
val foo3 = | |
if (someTest()) { | |
someFoo | |
} else { | |
throw new Exception("No value for `foo` provided!") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NOTE: Will not compile, since you explicitly specified your type. | |
// This is good, since it catches the error below. | |
val foo: Foo = | |
if (someTest()) { | |
someFoo | |
} else { | |
new Foo | |
// Developer adds println statement for debugging, but explicit type | |
// on Foo will catch that they changed the type of the expression. | |
println("Using default value for `foo`") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Foo foo; | |
if (someFoo != null) { | |
foo = someFoo; | |
} else { | |
foo = new Foo(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val foo = maybeFoo.getOrElse(new Foo) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val foo: Foo = | |
if (maybeFoo.isDefined) { | |
foo = maybeFoo.get // Option#get is generally a code smell. | |
} else { | |
foo = new Foo | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Map<Long, User> usersById = new HashMap<>(); | |
for (User user : users) { | |
usersById.put(user.getId(), user); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val usersById = { | |
val elems = users.map(u => (u.id, u)) | |
Map(elems: _*) // The _* syntax allows you to pass a sequence as a vararg | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def apply[A, B](elems: (A, B)*): Map[A, B] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val usersById = users.map(u => (u.id, u)).toMap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment