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)))
(ns most-frequent-words.test
(:require [clojure.test :refer :all]
[most-frequent-words.core :refer [most-frequent-words]]))
(deftest most-frequent-words-test
;; ROMEO AND JULIET
(def romeo-and-juliet-book-url "http://www.textfiles.com/etext/AUTHORS/SHAKESPEARE/shakespeare-romeo-48.txt")
;; OTHELLO
(def othello-book-url "http://www.textfiles.com/etext/AUTHORS/SHAKESPEARE/shakespeare-othello-47.txt")
;; THE WINTER'S TALE
(def the-winter-s-tale-book-url "http://www.textfiles.com/etext/AUTHORS/SHAKESPEARE/shakespeare-winters-19.txt")
(is (=
(most-frequent-words 7 romeo-and-juliet-book-url)
[["I" 656] ["the" 614] ["and" 490] ["to" 488] ["a" 411] ["of" 367] ["my" 314]]))
(is (=
(most-frequent-words 7 othello-book-url)
[["I" 892] ["the" 674] ["and" 596] ["to" 509] ["you" 453] ["of" 420] ["a" 397]]))
(is (=
(most-frequent-words 7 the-winter-s-tale-book-url)
[["the" 717] ["I" 713] ["and" 535] ["to" 517] ["of" 428] ["you" 399] ["a" 378]])))
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)
}
Hello world in Kotlin.
fun main(args: Array<String>) {
println("Hello from Kotlin")
}
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)))))
(ns factorial-test
(:require [clojure.test :refer :all]
[factorial :refer [get-factorial]]))
(deftest get-factorial-test
(is (get-factorial (- 10)) 0)
(is (get-factorial 0) 0)
(is (get-factorial 1) 1)
(is (get-factorial 2) 2)
(is (get-factorial 3) 6)
(is (get-factorial 4) 24)
(is (get-factorial 5) 120)
(is (get-factorial 10) 3628800)
(is (get-factorial 15) 1307674368000)
(is (get-factorial 20) 2432902008176640000))
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)))))
(ns fibonacci-test
(:require [clojure.test :refer :all]
[fibonacci :refer [get-fibonacci-number]]))
(deftest get-fibonacci-number-test
(is (get-fibonacci-number (- 10)) 0)
(is (get-fibonacci-number 0) 0)
(is (get-fibonacci-number 1) 1)
(is (get-fibonacci-number 2) 1)
(is (get-fibonacci-number 3) 2)
(is (get-fibonacci-number 4) 3)
(is (get-fibonacci-number 5) 5)
(is (get-fibonacci-number 10) 55)
(is (get-fibonacci-number 30) 832040))
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(' ')
- }
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
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(' ')
}
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()
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
}
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()}
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"
}