Skip to content

Instantly share code, notes, and snippets.

View parambirs's full-sized avatar
👋

Parambir Singh parambirs

👋
View GitHub Profile
@parambirs
parambirs / s4di_ch01_exercises.sc
Last active August 14, 2019 23:11
Solutions for "Scala for the Impatient", chapter 1 exercises
package src.exercises
import scala.math._
import BigInt.probablePrime
import util.Random
object chap01 {
// 1. In the Scala REPL, type 3. followed by the Tab key. What methods can be
// applied?
// => Do it in REPL. There are many methods including %, &, *, +, toByte, toChar etc.
// 1. The signum of a number is 1 if the number is positive, -1 if it is negative, and
// 0 if it is zero. Write a function that computes this value.
def signum(n: Int) = {
if (n > 0) 1
else if (n < 0) -1
else 0
} //> signum: (n: Int)Int
signum(4) //> res0: Int = 1
// 1. Write a code snippet that sets a to an array of n random integers between 0
// (inclusive) and n (exclusive).
val a = new Array[Int](10) //> a : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
for(i <- 0 until a.length) a(i) = scala.util.Random.nextInt(10)
a //> res0: Array[Int] = Array(9, 0, 5, 8, 6, 6, 3, 9, 0, 3)
// 2. Write a loop that swaps adjacent elements of an array of integers. For example,
// Array(1, 2, 3, 4, 5) becomes Array(2, 1, 4, 3, 5).
val a = Array[Int](1,2,3,4,5)
a //> res1: Array[Int] = Array(1, 2, 3, 4, 5)
// 1. Set up a map of prices for a number of gizmos that you covet. Then produce
// a second map with the same keys and the prices at a 10 percent discount.
val gizmos = Map("iPod" -> 20000, "iPhone" -> 45000, "iPad" -> 30000, "iMac" -> 85000)
//> gizmos : scala.collection.immutable.Map[java.lang.String,Int] = Map(iPod ->
//| 20000, iPhone -> 45000, iPad -> 30000, iMac -> 85000)
val discountedGizmos = for((k, v) <- gizmos) yield (k, v * 0.9)
//> discountedGizmos : scala.collection.immutable.Map[java.lang.String,Double]
//| = Map(iPod -> 18000.0, iPhone -> 40500.0, iPad -> 27000.0, iMac -> 76500.0)
// 2. Write a program that reads words from a file. Use a mutable map to count
@parambirs
parambirs / sfti_ch05_exercises.scala
Created January 4, 2015 02:11
Scala for the Impatient: Chapter 5 exercises solutions
// 1. Improve the Counter class in Section 5.1, "Simple Classes and Parameterless
// Methods," on page 51 so that it doesn't turn negative at Int.MaxValue.
class Counter {
private var value = 0
def increment() { if(value < Int.MaxValue) value += 1 }
def current = value
def isLess(other: Counter) = value < other.value // can access private field of other object
}
@parambirs
parambirs / sfti_ch06_exercises.scala
Last active January 7, 2017 03:01
Scala for the Impatient: Chapter 6 exercises solutions
//1. Write an object Conversions with methods inchesToCentimeters, gallonsToLiters, and
//milesToKilometers.
object Conversions {
def inchesToCentimeters(inches: Double) = inches * 2.54
def gallonsToLiters(gallons: Double) = gallons * 3.78541
def milesToKilometers(miles: Double) = miles * 1.60934
}
//1. Write an example program to demonstrate that
//package com.horstmann.impatient
//is not the same as
//package com
//package horstmann
//package impatient
// File: Demo1.scala
package com {
package horstmann {
@parambirs
parambirs / 0_reuse_code.js
Last active August 29, 2015 14:26
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@parambirs
parambirs / insult.scala
Created September 5, 2015 06:51
Scala insult generator
#!/usr/bin/env scala
import scala.collection.immutable.IndexedSeq
val alphabets = 'A' to 'Z'
val insult = if(args.length > 0) args(0).toUpperCase else "STUPID"
val heading = alphabets filterNot (insult contains(_))
println(getInsult)
#!/usr/bin/env scala
import scala.collection.immutable.IndexedSeq
import scala.io.StdIn
val version = "Scala Insult Generator v0.1"
val cmdArg = if(args.length > 0) Some(args(0).toUpperCase) else None
val result = cmdArg match {