Ad

Finds top 7 most frequent words in the book.

(ns most-frequent-words.core)

(defn most-frequent-words[number-of-words, book-url]
  "Finds top 7 most frequent words in the book"
  (->> 
    (slurp book-url)
    (re-seq #"\w+")
    (frequencies)
    (sort-by val >)
    (take number-of-words)))
Saka7Failed Tests

Data classes

In Kotlin data classes is classes which do nothing but only hold data.

The compiler automatically derives the following members from all properties declared in the primary constructor:

  • equals()/hashCode() pair,
  • toString() of the form "User(name=John, age=42)",
  • componentN() functions corresponding to the properties in their order of declaration,
  • copy() function (see below).
data class User(val id: Long, var email: String, var name: String)

fun main(args: Array<String>) {
    var user = User(1, "random@email.com", "randomName")
    
    // toString
    println(user)
    
    // equals
    println(user.equals(User(1, "random@email.com", "randomName")))
    
    // ComponentN
    println("user name is ${user.component3()}")
    
    // copy
    var newUser = user.copy(name="newName")
    println(newUser)
}
Saka7Failed Tests

Hello Kotlin

Fundamentals

Hello world in Kotlin.

fun main(args: Array<String>) {
    println("Hello from Kotlin")
}
Loops
Control Flow
Basic Language Features
Fundamentals
Arrays
Data Types

Simple example of loop types in bash:

  • for
  • while
  • until
#!/bin/bash
# some array
some_arr=('A', 'B', 'C')
# array length
len=${#some_arr[@]}

# for-in loop
echo "For In Loop"

for i in ${some_arr[*]}; do
  printf $i
done

# c-like for loop
echo -e "\nC-Like for Loop"

for((i=0;i<$len;i++)); do
  printf ${some_arr[$i]}
done;

# more c-like for loop
echo -e "\nMore C-like for loop"

for((i=0;i<$len;i++)) { 
  printf ${some_arr[i]}
}

# while loop
i=0
echo -e "\nWhile loop"

while [ $i -lt $len ]; do
  printf ${some_arr[$i]}
  let i+=1
done;

# until loop
j=0
echo -e "\nUntil loop"

until [ $j -ge $len ]; do
  printf ${some_arr[$j]}
  let j+=1
done

echo "THE END"

Finds factorial

(ns factorial)

(defn get-factorial [n]
  "takes n as a paramenter and returns n!"
  (if (<= n 0) 0)
  (if (<= n 1) 1
   (* n (get-factorial (dec n)))))

Finds Fibonacci number by given index

(ns fibonacci)

(defn get-fibonacci-number [index]
  "Finds fibonacci number by given index"
  (if (<= index 0) 0)
  (if (<= index 2) 1
    (+ (get-fibonacci-number (- index 1))
       (get-fibonacci-number (- index 2)))))
Lists
Data Structures
Strings
Data Types
Code
Diff
  • def name = "John Cena"
    try {
      println "Your name in japanese is " + convertToJapanese(name)
    } catch(e) {
      System.err << e.getMessage()
    }
    
    static String convertToJapanese(String name) {
    
        name = name.toLowerCase()
        
        if(name == "" || !name.matches('^[a-z\\s]*$'))
          throw new Exception('ERROR: invalid name\n')
    
        def alphabet = [
          'ka','zu','mi','te','ku',
          'lu','ji','ri','ki','zus',
          'me','ta','rin','to','mo',
          'no','ke','shi','ari','chi',
          'do','ru','mei','na','fu','zi'
        ]
    
        String japaneseName = ''
    
        name.each {
          if(it.matches('\\s'))
            japaneseName += ' '
          else 
            japaneseName += alphabet[((int)it) - 97]
        }
    
        japaneseName.split(' ').collect{it.capitalize()}.join(' ')
    }
    • def name = "John Cena"
    • try {
    • println "Your name in japanese is " + convertToJapanese(name)
    • } catch(e) {
    • System.err << e.getMessage()
    • }
    • static String convertToJapanese(String name) {
    • name = name.toLowerCase()
    • if(name == "" || !name.matches('^[a-z\\s]*$'))
    • throw new Exception('ERROR: invalid name\n')
    • def alphabet = [
    • 'ka','zu','mi','te','ku',
    • 'lu','ji','ri','ki','zus',
    • 'me','ta','rin','to','mo',
    • 'no','ke','shi','ari','chi',
    • 'do','ru','mei','na','fu','zi'
    • ]
    • String japaneseName = ''
    • name.each {
    • if(it in [' ', '\t', '\n'])
    • japanaseName += ' '
    • if(it.matches('\\s'))
    • japaneseName += ' '
    • else
    • japanaseName += alphabet[((int)it) - 97]
    • japaneseName += alphabet[((int)it) - 97]
    • }
    • japanaseName.split(' ').collect{it.capitalize()}.join(' ')
    • japaneseName.split(' ').collect{it.capitalize()}.join(' ')
    • }
Sets
Arrays
Data Types

Example how to use basic sets operations in groovy

Set a = [6, 3, 4, 2, 5, 8]
Set b = [8, 9, 7, 5, 2, 6]
println "Set A : ${a}"
println "Set B: ${b}"
println "A = B : ${a == b}" // equality
println "A + B : ${a + b}" // union
println "A - B : ${a - b}" // difference
println "A symmetric difference B : ${(a-b) + (b-a)}" // symmetric difference
println "A intersection B : ${a.intersect b}" // intersection
Lists
Data Structures
Strings
Data Types

Example how to convert your name to japanese in Groovy.(joke)

def name = "John Cena"
try {
  println "Your name in japanese is " + convertToJapanese(name)
} catch(e) {
  System.err << e.getMessage()
}

static String convertToJapanese(String name) {

    name = name.toLowerCase()
    
    if(name == "" || !name.matches('^[a-z\\s]*$'))
      throw new Exception('ERROR: invalid name\n')

    def alphabet = [
      'ka','zu','mi','te','ku',
      'lu','ji','ri','ki','zus',
      'me','ta','rin','to','mo',
      'no','ke','shi','ari','chi',
      'do','ru','mei','na','fu','zi'
    ]

    String japaneseName = ''

    name.each {
      if(it in [' ', '\t', '\n'])
        japanaseName += ' '
      else 
        japanaseName += alphabet[((int)it) - 97]
    }

    japanaseName.split(' ').collect{it.capitalize()}.join(' ')
}
Classes
Basic Language Features
Object-oriented Programming
Fundamentals
Programming Paradigms
class Person
  constructor: (@name, @age) ->
  toString: -> "#{@name} is #{@age} years old" 
  
class Developer extends Person
  constructor: (name, age, @skills) -> super(name, age)
  toString: -> super.toString() + " and has #{@skills} skills"
  
jack = new Developer("Jack", 20, ['CoffeeScript', 'JavaScript'])
console.log jack.toString()
Classes
Basic Language Features
Object-oriented Programming
Fundamentals
Programming Paradigms
Objects
Data Types
class Person {
  String name
  int age
  
  void setName(String name) {
    if(name.matches(/^[a-zA-Z]*$/))
      this.name = name
    else
      throw new Exception('Invalid name')
  }
  
  void setAge(int age) {
    if(age >= 0 && age <= 150)
      this.age = age
    else
      throw new Exception('Invalid age')
  }
  
  String toString() {
    "$name is $age years old"
  }
  
}

class Developer extends Person {
  List<String> skills
  
  Developer(String name="No Name", int age=0, List<String> skills=[]) {
    setName(name)
    setAge(age)
    this.skills = skills.collect()
  }
  
  String toString() {
    super.toString() + " and have $skills skills"
  }
  
}

static main(args) {
  Person jack = new Developer("jack", 25, ['Groovy on Grails'])
  jack.name = jack.name.toUpperCase()
  jack.age++
  jack.skills << 'Spring'
  
  try{
    jack.name = "~~~JACK~~~"
  } catch(e) {
    println "### Mistake: ${e.getMessage()} ###"
  }
  
  try{
    jack.age -= 200
  } catch(e) {
    println "### Mistake: ${e.getMessage()} ###"
  }
  
  print jack
}
Lists
Data Structures
Collections
def languages = ['Java', 'C#', 'Scala', 'F#']
languages.add('Groovy')
languages << 'Clojure'

languages.remove('F#')
languages -= 'C#'

println "JVM programming languages: "
languages.each{println it.toUpperCase()}
Maps
Data Structures
def frameworks = ['Groovy' : 'Grails', 'Java' : 'Spring', 'Scala' : 'Play']
  
frameworks.put('Ruby', 'Rails')
frameworks << ['Nodejs' : '...']

frameworks.remove('Ruby')

frameworks['Nodejs'] = 'Express'
  
frameworks.each {
  println "Most commonly used $it.key framework is $it.value"
}