SlideShare a Scribd company logo
Clojure Example Functions

                        ilegra - LDC
By Jackson dos Santos
odd
user =>(odd? 1)
true
odd

The same thing in java
(2 % 1) ? true : false
even
user => (even? 2)
true
even
The same thing in java
(2 % 2) ? true : false
dotimes
user => (dotimes [i 2] (println i))
0
1
2
dotimes
The same thing in java
Vector v = new Vector();
v.add(0);
v.add(1);
v.add(2);
for (int i = 0; i < v.size(); i++) {
System.out.println(v.get(i));
}
Car tax function
(defn verify_car_speed [car]
    (if (> 100 (get car :cur-speed)) true
        (do
            (def extraTax (* (- (get car :cur-speed) 100) 20))
            (assoc car :tax (+ extraTax 10))
        )
    )
)
Car tax tests
(deftest test-car-speed-with-less-than-100
  (def carToTest (struct car "FUCA" "PASSANGER" 99 1))
  (def carReturned (verify_car_speed carToTest))
  (is (= nil (get carReturned :tax)))
)

(deftest test-car-speed-with-more-than-100
  (def carToTest (struct car "FUCA" "PASSANGER" 101 1))
  (def carReturned (verify_car_speed carToTest))
  (is (= 30 (get carReturned :tax)))
)
Bank functions
(defn bank-deposit [bank-account value-to-deposit]
  (def value-to-credit (+ (get @bank-account :value) value-to-deposit))
  (dosync
    (ref-set bank-account (assoc @bank-account :value value-to-credit))
  )
)

(defn bank-withdraw[bank-account value-to-withdraw]
  (def value-to-dec (- (get @bank-account :value) value-to-withdraw))
  (dosync
    (ref-set bank-account (assoc @bank-account :value value-to-dec))
  )
)

(defn bank-assesment[bank-account]
  (str "Client " (get @bank-account :name)" has " (get @bank-account :value) " in his account")
)
Bank tests
(deftest test-deposit-100
  (def client-rich (ref (struct bank-account "Eike Batista" 100.02M)))
  (bank-deposit client-rich 100)
  (is (= 200.02 (get @client-rich :value)))
)

(deftest test-withdraw-99.03
  (def client-poor (ref (struct bank-account "Jackson" 224.07M)))
  (bank-withdraw client-poor 99.03M)
  (is (= 125.04 (get @client-poor :value)))
)

(deftest test-assesment
  (def rich-client (ref (struct bank-account "Silvio Santos" 1000)))
  (bank-deposit rich-client 100)
  (is (= "Client Silvio Santos has 1100 in his account" (bank-assesment rich-client)))
)
Ad

Recommended

PPTX
Thread base theory test
Llewellyn Falco
 
PPTX
JVM Architecture - Java
Ganesh kumar reddy
 
PDF
Property-based testing
Dmitriy Morozov
 
PDF
The Ring programming language version 1.2 book - Part 30 of 84
Mahmoud Samir Fayed
 
PDF
SISTEMA DE FACTURACION (Ejemplo desarrollado)
Darwin Durand
 
PDF
Clojure functions 4
Jackson dos Santos Olveira
 
DOCX
VISUALIZAR REGISTROS EN UN JTABLE
Darwin Durand
 
PDF
Redux saga: managing your side effects. Also: generators in es6
Ignacio Martín
 
DOCX
Detect Negative and Positive sentiment in user reviews using python word2vec ...
Mamoon Ismail Khalid
 
PDF
The Ring programming language version 1.5.2 book - Part 41 of 181
Mahmoud Samir Fayed
 
TXT
Empolyee deatils in java
DEEPAK KUMAR MAURYA
 
PDF
Visual Studio.Net - Sql Server
Darwin Durand
 
DOCX
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
Darwin Durand
 
PDF
Informatics Practices/ Information Practices Project (IP Project Class 12)
KushShah65
 
PPTX
LINQ Internals - STLDODN
Keith Dahlby
 
PDF
Clojure functions 3
Jackson dos Santos Olveira
 
PDF
Sistema de ventas
DAYANA RETO
 
PDF
Redux Sagas - React Alicante
Ignacio Martín
 
PDF
20180721 code defragment
Chiwon Song
 
PDF
History of jQuery
jeresig
 
PPTX
Using Redux-Saga for Handling Side Effects
GlobalLogic Ukraine
 
PPTX
The redux saga begins
Daniel Franz
 
PDF
Higher-Order Components — Ilya Gelman
500Tech
 
PDF
04 2 오버플로 상수 매크로
Changwon National University
 
PPTX
Process control nodejs
LearningTech
 
PDF
20181020 advanced higher-order function
Chiwon Song
 
PDF
Bindings: the zen of montage
Kris Kowal
 
KEY
Testing My Patience
Adam Lowry
 
PPTX
Scala meetup
扬 明
 
PDF
Clojure functions midje
Jackson dos Santos Olveira
 

More Related Content

What's hot (19)

DOCX
Detect Negative and Positive sentiment in user reviews using python word2vec ...
Mamoon Ismail Khalid
 
PDF
The Ring programming language version 1.5.2 book - Part 41 of 181
Mahmoud Samir Fayed
 
TXT
Empolyee deatils in java
DEEPAK KUMAR MAURYA
 
PDF
Visual Studio.Net - Sql Server
Darwin Durand
 
DOCX
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
Darwin Durand
 
PDF
Informatics Practices/ Information Practices Project (IP Project Class 12)
KushShah65
 
PPTX
LINQ Internals - STLDODN
Keith Dahlby
 
PDF
Clojure functions 3
Jackson dos Santos Olveira
 
PDF
Sistema de ventas
DAYANA RETO
 
PDF
Redux Sagas - React Alicante
Ignacio Martín
 
PDF
20180721 code defragment
Chiwon Song
 
PDF
History of jQuery
jeresig
 
PPTX
Using Redux-Saga for Handling Side Effects
GlobalLogic Ukraine
 
PPTX
The redux saga begins
Daniel Franz
 
PDF
Higher-Order Components — Ilya Gelman
500Tech
 
PDF
04 2 오버플로 상수 매크로
Changwon National University
 
PPTX
Process control nodejs
LearningTech
 
PDF
20181020 advanced higher-order function
Chiwon Song
 
PDF
Bindings: the zen of montage
Kris Kowal
 
Detect Negative and Positive sentiment in user reviews using python word2vec ...
Mamoon Ismail Khalid
 
The Ring programming language version 1.5.2 book - Part 41 of 181
Mahmoud Samir Fayed
 
Empolyee deatils in java
DEEPAK KUMAR MAURYA
 
Visual Studio.Net - Sql Server
Darwin Durand
 
INSERCION DE REGISTROS DESDE VISUAL.NET A UNA BD DE SQL SERVER
Darwin Durand
 
Informatics Practices/ Information Practices Project (IP Project Class 12)
KushShah65
 
LINQ Internals - STLDODN
Keith Dahlby
 
Clojure functions 3
Jackson dos Santos Olveira
 
Sistema de ventas
DAYANA RETO
 
Redux Sagas - React Alicante
Ignacio Martín
 
20180721 code defragment
Chiwon Song
 
History of jQuery
jeresig
 
Using Redux-Saga for Handling Side Effects
GlobalLogic Ukraine
 
The redux saga begins
Daniel Franz
 
Higher-Order Components — Ilya Gelman
500Tech
 
04 2 오버플로 상수 매크로
Changwon National University
 
Process control nodejs
LearningTech
 
20181020 advanced higher-order function
Chiwon Song
 
Bindings: the zen of montage
Kris Kowal
 

Similar to Clojure functions examples (20)

KEY
Testing My Patience
Adam Lowry
 
PPTX
Scala meetup
扬 明
 
PDF
Clojure functions midje
Jackson dos Santos Olveira
 
PPTX
Unit test candidate solutions
benewu
 
PDF
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
MongoSF
 
PDF
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Guy Royse
 
PDF
The Ring programming language version 1.2 book - Part 32 of 84
Mahmoud Samir Fayed
 
PPTX
Django - sql alchemy - jquery
Mohammed El Rafie Tarabay
 
ODP
Pruebas unitarias con django
Tomás Henríquez
 
PDF
The Ring programming language version 1.10 book - Part 54 of 212
Mahmoud Samir Fayed
 
PDF
Atomically { Delete Your Actors }
John De Goes
 
PDF
Phoenix for laravel developers
Luiz Messias
 
PDF
Testing in those hard to reach places
dn
 
PDF
Compose Async with RxJS
Kyung Yeol Kim
 
PDF
Scaling Up: How Switching to Apache Spark Improved Performance, Realizability...
Databricks
 
PDF
Scaling up data science applications
Kexin Xie
 
PPTX
Scott Anderson [InfluxData] | InfluxDB Tasks – Beyond Downsampling | InfluxDa...
InfluxData
 
PPT
Clojure in the Wild
suitzero
 
PDF
The Ring programming language version 1.7 book - Part 48 of 196
Mahmoud Samir Fayed
 
Testing My Patience
Adam Lowry
 
Scala meetup
扬 明
 
Clojure functions midje
Jackson dos Santos Olveira
 
Unit test candidate solutions
benewu
 
Rails-like JavaScript Using CoffeeScript, Backbone.js and Jasmine
Raimonds Simanovskis
 
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
MongoSF
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
Guy Royse
 
The Ring programming language version 1.2 book - Part 32 of 84
Mahmoud Samir Fayed
 
Django - sql alchemy - jquery
Mohammed El Rafie Tarabay
 
Pruebas unitarias con django
Tomás Henríquez
 
The Ring programming language version 1.10 book - Part 54 of 212
Mahmoud Samir Fayed
 
Atomically { Delete Your Actors }
John De Goes
 
Phoenix for laravel developers
Luiz Messias
 
Testing in those hard to reach places
dn
 
Compose Async with RxJS
Kyung Yeol Kim
 
Scaling Up: How Switching to Apache Spark Improved Performance, Realizability...
Databricks
 
Scaling up data science applications
Kexin Xie
 
Scott Anderson [InfluxData] | InfluxDB Tasks – Beyond Downsampling | InfluxDa...
InfluxData
 
Clojure in the Wild
suitzero
 
The Ring programming language version 1.7 book - Part 48 of 196
Mahmoud Samir Fayed
 
Ad

More from Jackson dos Santos Olveira (20)

PDF
AWS Control Tower
Jackson dos Santos Olveira
 
PDF
Netty training
Jackson dos Santos Olveira
 
PDF
An introduction to predictionIO
Jackson dos Santos Olveira
 
PDF
Introduction to HashiCorp Consul
Jackson dos Santos Olveira
 
PDF
Apache mahout - introduction
Jackson dos Santos Olveira
 
PDF
Managing computational resources with Apache Mesos
Jackson dos Santos Olveira
 
PDF
Introduction to CFEngine
Jackson dos Santos Olveira
 
PDF
DBC Principles
Jackson dos Santos Olveira
 
PDF
Jboss Teiid - The data you have on the place you need
Jackson dos Santos Olveira
 
PDF
Apache PIG introduction
Jackson dos Santos Olveira
 
PPSX
Jboss AS7 New Main Features
Jackson dos Santos Olveira
 
PPSX
Celery Introduction
Jackson dos Santos Olveira
 
PPT
Elastic search introduction
Jackson dos Santos Olveira
 
AWS Control Tower
Jackson dos Santos Olveira
 
An introduction to predictionIO
Jackson dos Santos Olveira
 
Introduction to HashiCorp Consul
Jackson dos Santos Olveira
 
Apache mahout - introduction
Jackson dos Santos Olveira
 
Managing computational resources with Apache Mesos
Jackson dos Santos Olveira
 
Introduction to CFEngine
Jackson dos Santos Olveira
 
Jboss Teiid - The data you have on the place you need
Jackson dos Santos Olveira
 
Apache PIG introduction
Jackson dos Santos Olveira
 
Jboss AS7 New Main Features
Jackson dos Santos Olveira
 
Celery Introduction
Jackson dos Santos Olveira
 
Elastic search introduction
Jackson dos Santos Olveira
 
Ad

Recently uploaded (20)

PDF
Shocks and Inequality An Empirical Exploration
GRAPE
 
PDF
Family Owned Business Succession/Estate Planning
imccci
 
PDF
Making History Accessible Shivsrushti’s ₹50 Entry Initiative Backed by Abhay ...
Harsh Mishra
 
DOCX
The Ultimate Guide to Buy Verified LinkedIn Accounts.docx
Buy Verified Linkedin Accounts
 
PPTX
Questions for FCRA Seminar latest Dec 12, 2023
imccci
 
PPTX
一比一原版(MP毕业证)墨尔本理工学院毕业证如何办理
taqyed
 
PPTX
Issues pertaining to Penalty, Prosecution and Compounding procedures under TD...
imccci
 
PPTX
RECENT DEVELOPMENT IN TAXATION OF CHARITABLE TRUST
imccci
 
PDF
Global Islamic Fintech Report 2024 /2025
Amine أمين Chakir شكير
 
PDF
Making Heritage Inclusive at Shivsrushti - How a single donation made cultura...
Raj Kumble
 
PPTX
最新版美国威斯康星大学密尔沃基分校毕业证(UWM毕业证书)原版定制
taqyea
 
PDF
How Zenko Properties Streamlined Financial Operations and Scaled Efficiently ...
Ratiobox Limited
 
PDF
London’s Leading Finance Courses: Unlock Your Career Potential
Finex Learning
 
PDF
Who is required to file form 10B/10BB – Rule 16CC and Rule 17B
imccci
 
PDF
Why Alternative Payment Methods Are Changing the International Business Lands...
PayXBorder
 
PPTX
Prospects & Challenges of Doing Business in Nepal: Investment Opportunities, ...
Kshitiz Parajuli
 
PPTX
Pieter Stalenhoef Leadership in Finance, Lifestyle Balance, and the Value of ...
WilliamClack2
 
PPTX
C.H. BHABHA MEMORIAL ENDOWMENT PUBLIC MEETING ON ANALYSIS OF UNION BUDGET 202...
imccci
 
PPTX
Panel 2 - Panel discussion - Demystifying key critical & practical issues und...
imccci
 
PPTX
STOCK TRADING COURSE BY FINANCEWORLD.IO (PDF)
AndrewBorisenko3
 
Shocks and Inequality An Empirical Exploration
GRAPE
 
Family Owned Business Succession/Estate Planning
imccci
 
Making History Accessible Shivsrushti’s ₹50 Entry Initiative Backed by Abhay ...
Harsh Mishra
 
The Ultimate Guide to Buy Verified LinkedIn Accounts.docx
Buy Verified Linkedin Accounts
 
Questions for FCRA Seminar latest Dec 12, 2023
imccci
 
一比一原版(MP毕业证)墨尔本理工学院毕业证如何办理
taqyed
 
Issues pertaining to Penalty, Prosecution and Compounding procedures under TD...
imccci
 
RECENT DEVELOPMENT IN TAXATION OF CHARITABLE TRUST
imccci
 
Global Islamic Fintech Report 2024 /2025
Amine أمين Chakir شكير
 
Making Heritage Inclusive at Shivsrushti - How a single donation made cultura...
Raj Kumble
 
最新版美国威斯康星大学密尔沃基分校毕业证(UWM毕业证书)原版定制
taqyea
 
How Zenko Properties Streamlined Financial Operations and Scaled Efficiently ...
Ratiobox Limited
 
London’s Leading Finance Courses: Unlock Your Career Potential
Finex Learning
 
Who is required to file form 10B/10BB – Rule 16CC and Rule 17B
imccci
 
Why Alternative Payment Methods Are Changing the International Business Lands...
PayXBorder
 
Prospects & Challenges of Doing Business in Nepal: Investment Opportunities, ...
Kshitiz Parajuli
 
Pieter Stalenhoef Leadership in Finance, Lifestyle Balance, and the Value of ...
WilliamClack2
 
C.H. BHABHA MEMORIAL ENDOWMENT PUBLIC MEETING ON ANALYSIS OF UNION BUDGET 202...
imccci
 
Panel 2 - Panel discussion - Demystifying key critical & practical issues und...
imccci
 
STOCK TRADING COURSE BY FINANCEWORLD.IO (PDF)
AndrewBorisenko3
 

Clojure functions examples

  • 1. Clojure Example Functions ilegra - LDC By Jackson dos Santos
  • 3. odd The same thing in java (2 % 1) ? true : false
  • 5. even The same thing in java (2 % 2) ? true : false
  • 6. dotimes user => (dotimes [i 2] (println i)) 0 1 2
  • 7. dotimes The same thing in java Vector v = new Vector(); v.add(0); v.add(1); v.add(2); for (int i = 0; i < v.size(); i++) { System.out.println(v.get(i)); }
  • 8. Car tax function (defn verify_car_speed [car] (if (> 100 (get car :cur-speed)) true (do (def extraTax (* (- (get car :cur-speed) 100) 20)) (assoc car :tax (+ extraTax 10)) ) ) )
  • 9. Car tax tests (deftest test-car-speed-with-less-than-100 (def carToTest (struct car "FUCA" "PASSANGER" 99 1)) (def carReturned (verify_car_speed carToTest)) (is (= nil (get carReturned :tax))) ) (deftest test-car-speed-with-more-than-100 (def carToTest (struct car "FUCA" "PASSANGER" 101 1)) (def carReturned (verify_car_speed carToTest)) (is (= 30 (get carReturned :tax))) )
  • 10. Bank functions (defn bank-deposit [bank-account value-to-deposit] (def value-to-credit (+ (get @bank-account :value) value-to-deposit)) (dosync (ref-set bank-account (assoc @bank-account :value value-to-credit)) ) ) (defn bank-withdraw[bank-account value-to-withdraw] (def value-to-dec (- (get @bank-account :value) value-to-withdraw)) (dosync (ref-set bank-account (assoc @bank-account :value value-to-dec)) ) ) (defn bank-assesment[bank-account] (str "Client " (get @bank-account :name)" has " (get @bank-account :value) " in his account") )
  • 11. Bank tests (deftest test-deposit-100 (def client-rich (ref (struct bank-account "Eike Batista" 100.02M))) (bank-deposit client-rich 100) (is (= 200.02 (get @client-rich :value))) ) (deftest test-withdraw-99.03 (def client-poor (ref (struct bank-account "Jackson" 224.07M))) (bank-withdraw client-poor 99.03M) (is (= 125.04 (get @client-poor :value))) ) (deftest test-assesment (def rich-client (ref (struct bank-account "Silvio Santos" 1000))) (bank-deposit rich-client 100) (is (= "Client Silvio Santos has 1100 in his account" (bank-assesment rich-client))) )