Skip to main content

Posts

Showing posts with the label programming

Elixir: Pattern Matching - A Taste of Functional Programming

=  is a match operator After an initial assignment it then becomes a match assertion. num = 1 To check the match assertion you can try this and it will be valid 1 = num Furthermore, these are also valid: [1] = [1] [num] = [num] Destructuring [score1, score2, score3] = [89, 93, 87] IO.puts score1  # 89 IO.puts score2  # 93 IO.puts score3  # 87 {:ok, value} = {:ok, 1000} IO.puts value # 1000 {:error, message} = {:error, "Uh oh"} IO.puts message # "Uh oh" Error when matching: {:foo, value} = {:bar, "nope"} ** (MatchError) no match of right hand side value: {:bar, "nope"} Matching function arguments Various function definitions and subsequent calls: def sum_two_nums(num1, num2) do num1 + num2 end sum_two_nums(2,5) def sum_two_nums(%{num1: num1, num2: num2}) do num1 + num2 end sum_two_nums(%{num1: 2, num2: 5}) def sum_two_nums([num1, num2]) do num1 + num2 end sum_two_nums([2,5]) There's so much more you ...

Elixir: Anonymous Functions - A Taste of Functional Programming

An anonymous function is composed of an optional parameter list, a body enclosed by fn -> and end . It returns a function definition and the potential for the function to be executed. Quite simply the anonymous function can be assigned to a variable which then can be subsequently called. Define an anonymous function:, greet = fn -> IO.puts "Hello World" end Of course that does nothing unless we call it! We can do that by using this dot notation - [function name].([optional parameters]) Like this: greet.() Where the output is: Hello World Anonymous functions with parameters add = fn(num1, num2) -> num1 + num2 end IO.puts add.(10, 15) IO.puts add.(12, 15) IO.puts add.(14, 19) subtract = fn num1, num2 -> num1 - num2 end IO.puts subtract.(15, 10) Output: 25 27 33 5 I hope these examples ignite an interest for you to further learn functional programming using Elixir. Cheers! Other posts in this series: A Taste of Functional Programming A...

AngularJS 101: A Beginner's Tutorial

Below is the PDF article I submitted to Software Developer's Journal  for their series they did on AngularJS. It was published in two of their issues: Nov. 15th 2013 -  AngularJS, Java and Drupal Tips & Tricks and Nov. 7th 2013 -  AngularJS Starter Kit . It's behind a paywall and thus hidden from most of the world. That sucks... which is why I'm posting my article here for all to see. Enjoy! AngularJS 101: A Beginner's Tutorial Github code for application built in the tutorial

vim.merge!(rails)

After meeting tpope and pair programming with reinh I got to see the power of vim . I have never used vim before so this is all new to me. I've been using TextMate exclusively since starting Rails a few years ago. I came to work and started in on a pursuit of how I can do in vim what I do in Textmate. While on this quest, I will document along the way some useful tips that may help you too. I'll document some vim things I've learned so far: Install rails.vim Other plugins: BlockComment , NERDTree , FuzzyFinderTextmate , minibufexpl , vcscommand Vim commands I use often: gf - when cursor is on class name it'll take you to that class definition yyp - duplicate current line :AS - I use a lot to look at code and spec at the same time ^p - autocomplete word from any open file control + shift + 6 - switch between two buffers shift + s - to delete current line :%s/old/new/g - to change text to new text / - to go to line number on page /text, then enter key - to f...

Ruby Chops: Flexible Sort

So I was messing with the comparable module and the method.  I implemented as I would in Java.  Then I played with sort_by and thought that was awesome. Which led me to writing my own method that would allow me to pass in a collection and attributes to be sorted by. The objects in the collection would have to implement my Sortable module. This was more for academic fun and try to keep my mind sharp. If anyone sees a cooler way to do it then please post it here.

Code Documentation

There are two ways to communicate what your code does: the code itself and using comments. Self Documenting Code Source code should be understandable not because it has comments but because of its elegance and clarity – correct use of variable names, good use of whitespace, good separation of logic, and concise readability. Code will be read hundreds of times and written only a few times. So invest quality time into spelling out names such as fn into firstname . Fight the urge to use cryptic variable names. Code Comments Code should have comments however an abundance of comments can be just as bad as too few. Comments should explain why something is done. The code itself already shows what it is done. So commenting on what is done is redundant. Do not use commenting as a substitute for good code. Another way to say that is that if you need to comment code due to its unclarity then maybe you need to rewrite the code to be more clear and remove the comment. I found this article ...