Today I will be looking at exception handling. Clojure provides the normal try-catch-finally block to catch exceptions and has the following general syntax
Tuesday, September 15, 2020
#100DaysOfClojure (Day [14] flow-control-cont-3)
Monday, September 14, 2020
#100DaysOfClojure (Day [13] flow-control-cont-2)
My apologies, as fun as Clojure is, I managed to drag myself away and took a 2 day break. Today I will look at recursion in Clojure. First let's I will look at recursion as used in most non functional languages where the function is called within the function(the function calls itself). But this usually only works up to the size of the stack frame. Lets look at a factorial function as a recursive example (The N at the end of the number just lets Clojure REPL know that it is a Clojure BigInt)
Friday, September 11, 2020
#100DaysOfClojure (Day [12] flow-control-cont)
(println “Bye 4 Now!”)
Thursday, September 10, 2020
#100DaysOfClojure (Day [11] flow-control)
In Clojure everything is an expression because everything returns a value. Even a block of multiple expressions will return the last value. There are 3 basic flow operators: if, when and do, which themselves are also expressions.
Wednesday, September 09, 2020
#100DaysOfClojure (Day [10] Sets)
A Set is a collection with unique elements. They are like mathematical sets, unordered and unique. They are used for checking whether a collection contains an element or removing an arbitrary element. You will notice they can contain any type including functions.
Tuesday, September 08, 2020
#100DaysOfClojure (Day [9] Maps)
Today I will look at Maps, one of the most important collection types in Clojure. Maps are collections of key-value pairs. Keys can be of any type in Clojure including functions, lists, other maps etc. The are 3 types of maps in Clojure: Hash Maps, Sorted Maps and Array Maps. Sorted maps keep the key value pairs in a certain order which implies that a lookup time may not be constant and it also allows you to traverse the values in the order of the keys. Sorted maps have a constraint in that the keys have to be comparable.
Monday, September 07, 2020
#100DaysOfClojure (Day [8] Vectors)
Vector is another collection data structure that allow random access and add new elements to the end of the collection.
Sunday, September 06, 2020
#100DaysOfClojure (Day [7] (lists))
The Clojure programming language is designed around data processing which includes the code as data. Today I get into the very foundations of programs in Clojure, the collection data structures. Remember that data in Clojure is immutable so in order for Collections to be efficient they need to be persistent, which means that all the copies have to share the common data. The are 4 collection types, Lists, Vectors, Maps and Sets. And these collections share a set of core functions :
- conj - returns a new list with the new add item added
- seq - returns sequence to of the collection
- count - return the number of items in a collection
- empty - returns an empty collection of the same type
- = - determine the value equality of one or more collections
Saturday, September 05, 2020
#100DaysOfClojure (Day [6] Functions Continued)
Today I am continuing with 3 more useful function concepts: How to define functions, anonymous functions and using apply with functions
Friday, September 04, 2020
#100DaysOfClojure (Day [5] Functions)
The excitement is building up, today I played with functions. A Clojure function has the following structure
Thursday, September 03, 2020
#100DaysOfClojure (Day [4] Basics)
Today I can dip my toes into the language itself. I will be looking at the some of the syntax and forms, Clojure refers to valid code as forms. Clojure has literals which consist of primitive data types, symbols and collections.
Wednesday, September 02, 2020
#100DaysOfClojure (Day [3] IntelliJ + Cursive)
With the wide variety of editors/ides available, it was a difficult decision to make. So how did I make this decision you ask? I made the decision based on the quality of the documentation and my familiarity with the editor. Hence I chose IntelliJ and the Cursive plugin. Cursive provide 3 licenses, personal, commercial and non-commercial. Since I am only using it to learn Clojure, I chose the non-commercial licence which is a free 6 month renewable licence.
Tuesday, September 01, 2020
#100DaysOfClojure : (+ Day [2] Tools)
Today was a busy and tiring day, so I decided to keep it light and explore the tools available for Clojure. The first tool of importance is Leiningen. It is a build automation and dependency management tool for the Clojure programming language written in Clojure itself. It integrates well with Maven repositories and the Clojars repository for Clojure libraries. On an Apple Mac you can install Leiningen very easily using homebrew.
- The ability to create projects
- Compile and package projects
- Run projects
- Run tests
- And more….
- Emacs with Cider plugin
- Atom with nrepl
- Cursive plugin for IntelliJ
- Nightcode
- Counterclockwise a plugin for Eclipse
- Vim with plugins
- Light table
- SublimeText with SublimeREPL
- Netbeans Clojure
- VS Code with Calva plugin
Monday, August 31, 2020
#100DaysOfClojure : (#Day [1] Intro)
Tuesday, November 08, 2016
I Am A Failed Consultant
Tuesday, January 14, 2014
JavaScript: Splicing an Array
It has the following syntax
array.splice(index, quantity, item1, ..., itemX) returning an array
where the parameters are defined as:
index : it is required and specifies the position at which to add or remove items. If it is negative it will calculate from the end of the array.
quantity : it is optional and specifies the number of items to be removed from the index onwards. To not remove any items, set it to 0. If it is not specified it will remove all elements after the index.
itemX : it is optional and is the items to be added to the array at the specified index.
Lets define an array
var myArray = [1,2,3,4,5]
Now lets remove all elements from index 4 onwards
myArray.splice(4)
This results in the following array
[ 1, 2, 3, 4 ]
Now let us add some elements to our array starting at index 4,
myArray.splice(4,0,5,6,7,8,9,10)
This results in the following array
[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
Now lets replace elements with values 4 and 5 with 11 and 12
myArray.splice(3,2,11,12) //remove 2 elements at index 3 and add 11 and 12 at index 3
This results in the following array
[ 1, 2, 3, 11, 12, 6, 7, 8, 9, 10 ]
Now lets remove elements with values 11 and 12 from the back of the array
myArray.splice(-7,2)
This results in the following array
[ 1, 2, 3, 6, 7, 8, 9, 10 ]
Pretty cool hey, that's enough on splicing, its time for you to experiment with it. Till next time enjoy!!!
Monday, September 09, 2013
JavaScript: Slicing an Array
Suppose I have an array
var myArray = [1,2,3,4,5];
and I want the first 3 elements, I can do the following
myArray.slice(0,3);
which return an array with the first 3 elements
[ 1, 2, 3 ]
This does not modify the original array in any way, it just returns a new array
The function slice has the following signature,
slice ( beginIndex, endIndex ) where the index is zero based i.e. the first element in a an array starts at index 0.
You could also access the array from the end using negatives indices for example,
myArray.slice(-3, -1);
returns
[ 3, 4 ]
Next we will look at splice......until next time!
Monday, July 22, 2013
JavaScript: Length of Arrays
var a = [];
a.length //return 0, a is an empty array
var b = [2,4,6];
b.length //return 3
b[3] = 8;
b.length //returns 4
b['a'] = 5; //never a good idea to use an array as an associative array, instead use a plain object
b.length //returns 4, non numeric indices are ignored in the length property
b[10000] = 50;
b.length //returns 10001, the positions between 3 and 10000 are padded with undefined
Setting the length of an array will shorten it as well
b.length = 4
b.length //returns 4, shortened the array from 10001 to 4
The safest way to empty an array is to set the length to 0
b.length = 0; //empty's an array
Next, I will be looking at the slice and splice functions on an array, till next time.......
Friday, July 12, 2013
JavaScript: More on Arrays
- length - return the total number of elements in the array
- join - concatenates all the elements in the array into a single string separated by the parameter passed
- concat - concatenates an array to the array and returns a new array
- sort - sorts the array by character encoding by default or by a user defined function
- reverse - reverses the elements of the array
- slice - returns an array that is a subset of the original array
- splice - can insert, remove or replace one or more array elements
- push - adds an element to the end of an array
- pop - it removes and returns the last element of an array
- shift - it removes and returns the first element of the array shifting all other elements down
- unshift - inserts elements at the beginning of an array shifting all the other element up
- toString - converts all the elements to a string and concatenates them to a comma delimited string
- toLocaleString - converts all the elements to a localised string and concatenates them to a comma delimited string
Tuesday, July 02, 2013
JavaScript: Arrays
var x = new Array (1, 5, "ten", "twenty");
or
var y = [1, 'a', true, 7.5, new Object()];
or
var z = [ [1, "hello"], [2, {name:"test"}]];
Array indexes are zero based and can be accessed as follows
var value = x[0] //value will be 1
var anotherValue = z[1][1]['name'] //anotherValue will be test
Array indexes are generally integers greater than or equal to 0 and less than 232 - 1, anything else would be converted to a string and looked up as a string.
Arrays can also act as a dictionary with ease, using the x variable defined above, you could do the following
x['a'] = 12345
Yip the power of arrays in Javascript is terrifying, but liberating. I will look at some other array functionality in the next instalment


