Mostrando entradas con la etiqueta LEDNumbers. Mostrar todas las entradas
Mostrando entradas con la etiqueta LEDNumbers. Mostrar todas las entradas

martes, 14 de febrero de 2017

LED is my new Hello World - Prolog time

As promised...here's my LED Numbers app written in Prolog...it took me a long time...a lot of research...a lot of headaches...so I hope you like it...still a complete Prolog newbie...so...no warranties at all -;)

LEDNumbers.pl

number(0,[[' _  '],['| | '],['|_| ']]).
number(1,[['  '],['| '],['| ']]).
number(2,[[' _  '],[' _| '],['|_  ']]).
number(3,[['_  '],['_| '],['_| ']]).
number(4,[['    '],['|_| '],['  | ']]).
number(5,[[' _  '],['|_  '],[' _| ']]).
number(6,[[' _  '],['|_  '],['|_| ']]).
number(7,[['_   '],[' |  '],[' |  ']]).
number(8,[[' _  '],['|_| '],['|_| ']]).
number(9,[[' _  '],['|_| '],[' _| ']]).

digits(0,[]).
digits(X,[H|T]) :- (X/10 > 0 -> H1 is floor(X/10), H is X mod 10, digits(H1,T)), !.

accRev([],A,A).
accRev([H|T],A,R) :- accRev(T,[H|A],R). 

getDigits(L,R) :- digits(L,Y), accRev(Y, [], R).

show_records([]).
show_records([A|B]) :-
  print_records(A), nl,
  show_records(B).  

print_records([]).
print_records([A|B]) :-
  format('~w',A), 
  print_records(B).

merge([L], L).
merge([H1,H2|T], R) :- maplist(append, H1, H2, H),
    merge([H|T], R), !.

listnum([],[]).
listnum([H1|T1],[R|Y]) :- number(H1,R), listnum(T1,Y).

led(X) :- getDigits(X,Y), listnum(Y,Z), merge(Z,R), show_records(R).

Wanna see it in action? Me too -;)


Back to learning -;)

Greetings,

Blag.
Development Culture.

lunes, 5 de diciembre de 2016

LED is my new Hello World - Rust time

As I'm currently learning Rust, I need to publish my LED app again -;)

Please take in mind...that..."I'm learning Rust"...so my code might be buggy, long and not idiomatic...but...enough to showcase the language and allow me to learn more -;)

Here's the code...

led_numbers.rs
use std::io;
use std::collections::HashMap;

fn main(){
 let mut leds:HashMap<&str, &str> = HashMap::new();

 leds.insert("0", " _  ,| | ,|_| ");
 leds.insert("1", "  ,| ,| ");
 leds.insert("2", " _  , _| ,|_  ");
 leds.insert("3", "_  ,_| ,_| ");
 leds.insert("4", "    ,|_| ,  | "); 
 leds.insert("5", " _  ,|_  , _| ");
 leds.insert("6", " _  ,|_  ,|_| ");
 leds.insert("7", "_   , |  , |  ");
 leds.insert("8", " _  ,|_| ,|_| ");
 leds.insert("9", " _  ,|_| , _| ");

 println!("Enter a number : ");
 let mut input_text = String::new();
 io::stdin().read_line(&mut input_text)
            .expect("failed to read");

 let split = input_text.split("");
 let vec: Vec<&str> = split.collect();
 let count = &vec.len() - 2;
 
 for i in 0..3{
  for j in 0..count{
   match leds.get(&vec[j]){
    Some(led_line) => { 
     let line = led_line.split(",");
     let vec_line: Vec<&str> = line.collect();
     print!("{}",&vec_line[i]);
     },
    None => println!("")
   }
  }
  print!("");
 }
 println!("");
}

And here's the result...


Hope you like and if you can point me in more Rusty way of doing it...please let me know -:D

Greetings,

Blag.
Development Culture.

jueves, 1 de diciembre de 2016

LED is my new Hello World - Swift (for Linux) time

It took me some time to write this post...mainly because I'm now learning Rust and also because I just finished my latest demo...whose blog is coming later today -;)

This version of my LED Numbers app becomes the 25th language version...so...obviously...it's a pretty nice milestone for me -:D Who knows? Maybe I will do something nice if I can ever reach 50 languages -:D

Anyway...like I love to say..."Enough talk...show me the source code" -;)

LedNumbers.swift
let leds: [Character:String] = [
 "0" : " _  ,| | ,|_| ",
 "1" : "  ,| ,| ",
 "2" : " _  , _| ,|_  ",
 "3" : "_  ,_| ,_| ",
 "4" : "    ,|_| ,  | ",
 "5" : " _  ,|_  , _| ",
 "6" : " _  ,|_  ,|_| ",
 "7" : "_   , |  , |  ",
 "8" : " _  ,|_| ,|_| ",
 "9" : " _  ,|_| , _| "
];

print("Enter a number: ",terminator:"");
let num = readLine(strippingNewline: true);

var line = [String]();
var led = "";

for i in 0...2{
 for character in num!.characters{
  line = String(leds[character]!)!.
                       characters.split(separator: ",").map(String.init);
  print(line[i], terminator:"");
 }
 print("");
}

And here's the picture of it working its magic -:)


Greetings,

Blag.
Development Culture.

martes, 15 de noviembre de 2016

LED is my new Hello World - Perl Time

As promised...here's my LED Numbers a la Perl...and as always...please keep in mind that I'm Perl newbie...I know that there are more efficient, short and concise way of doing this app...but...how good is an introductory code that uses some obscure and arcane code? I don't want to scare people away from Perl...I want people to say "Hey...that doesn't look hard...I want to learn Perl"...

So...here it is...

LedNumbers.pl
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;

my %leds = (
 0 => ' _  ,| | ,|_| ',
 1 => '  ,| ,| ',
 2 => ' _  , _| ,|_  ',
 3 => '_  ,_| ,_| ',
 4 => '    ,|_| ,  | ',
 5 => ' _  ,|_  , _| ',
 6 => ' _  ,|_  ,|_| ',
 7 => '_   , |  , |  ',
 8 => ' _  ,|_| ,|_| ',
 9 => ' _  ,|_| , _| '
);

print "Enter a number: ";
my $num = <>;
my @numbers = ( $num =~ /\d/g );

for my $i (0 .. 2){
 for my $j (0 .. scalar(@numbers) - 1){
  my @line = split /\,/,$leds{$numbers[$j]};
  print $line[$i];
 }
 print "\n";
}

And here's the output...


And just so you know...this is my 24th version of this code...yep...I have written my LED Numbers app in 24 languages so far -;) What's going to be my end point? Who knows...programming is the limit -;)

Greetings,

Blag.
Development Culture.

lunes, 7 de diciembre de 2015

LED is my new Hello World - Elm Time

So...as promised...here's my LED Numbers app written in Elm -:)

Now...this took me more time than expected, due to a couple of things...

a) Elm likes to handle Just and Maybe values...Haskell does the same...but Haskell provide alternative functions that deal with Just and Maybe...Elm does not...so I need to come up with some functions for it...

b) Elm doesn't provide a function to get elements from a list...so I need to build the function...

c) I'm still an Elm newbie

That put aside...Elm is still awesome and I love it -;)

Anyway...let's get to work...

Create an folder called LED_Numbers and type this into the terminal...

elm package install evancz/elm-html

elm package install evancz/start-app

Then, open your favorite editor and copy and paste the code...

LED_Numbers.elm
module LED_Numbers where

import Html exposing (..)
import Html.Events exposing (..)
import Html.Attributes exposing (..)
import StartApp.Simple as StartApp
import String exposing (toInt,toList)
import Dict

--MODEL
type alias Model =
  { 
    number: String,
    leds: Dict.Dict Char (List String),
    line: String
  }

initialModel: Model
initialModel = 
  {
    number = "",
    leds = Dict.fromList[('0',[" _  ","| | ","|_| "]),('1',["  ","| ","| "]),
                         ('2',[" _  "," _| ","|_  "]),('3',["_  ","_| ","_| "]),
                         ('4',["    ","|_| ","  | "]),('5',[" _  ","|_  "," _| "]),
                         ('6',[" _  ","|_  ","|_| "]),('7',["_   "," |  "," |  "]),
                         ('8',[" _  ","|_| ","|_| "]),('9',[" _  ","|_| "," _| "])],
    line = ""
  }

--UPDATE
fromJust : Maybe a -> a
fromJust x = case x of
  Just y -> y
  Nothing -> Debug.crash ""

fromMaybe : Maybe (List String) -> List String
fromMaybe x = case x of
  Just y -> y
  Nothing -> Debug.crash ""

fromMaybeChar : Maybe Char -> Char
fromMaybeChar x = case x of
  Just y -> y
  Nothing -> Debug.crash ""

get_elem : List a -> Int -> Maybe a
get_elem lst n =
  List.head (List.drop n lst)

fromMaybeListChar : Maybe (List Char) -> List Char
fromMaybeListChar x = case x of
  Just y -> y
  Nothing -> Debug.crash ""

get_list: String -> List Char
get_list str =
  String.toList str

type Action = NoOp | Submit | UpdateNumber String

update: Action -> Model -> Model
update action model =
  case action of
    NoOp ->
      model
    UpdateNumber contents ->
      { model | number = contents }  
    Submit ->
      { model | 
          line = get_led (get_list model.number) 0 model,
          number = ""
      }

get_led: List Char -> Int -> Model -> String
get_led lst n model =
  if List.length lst > 0
  then let h = fromMaybeChar(List.head lst)
           t = fromMaybeListChar(List.tail lst)
           leds = model.leds
           line = Dict.get h leds
       in fromJust(get_elem (fromMaybe line) n) ++ get_led t n model
  else if n < 2
  then "" ++ "\n" ++ get_led (get_list model.number) (n+1) model
  else if n == 2
  then "" ++ "\n"
  else ""

--VIEW
buttonStyle: Attribute
buttonStyle = 
  style
    [ ("outline", "none"),
      ("border", "none"),
      ("border-radius","4px"),
      ("margin-right","5px"),
      ("padding","4px 10px"),
      ("background","#61a1bc"),
      ("color","#fff")
    ]

divStyle: Attribute
divStyle = 
  style
    [ ("margin", "50px auto"),
      ("padding", "0px 50px"),
      ("text-align", "center")
    ]

pStyle: Attribute
pStyle = 
  style
    [ ("font-size", "30px") ]

pageHeader : Html
pageHeader = 
  h1 [ ] [text "LED Numbers"]

view: Signal.Address Action -> Model -> Html
view address model = 
  div [ divStyle ]
    [ pageHeader,
      input
        [ 
          type' "text",
          name "number",
          placeholder "Enter a number",
          value model.number,
          on "input" targetValue (\v -> Signal.message address (UpdateNumber v))
        ]
        [ ],
      button [ buttonStyle, onClick address Submit ] [ text "Submit" ],
      pre [ pStyle ]
          [ text (model.line) ]
    ]
    
main: Signal Html
main = 
  StartApp.start { model = initialModel, view = view, update = update }

Go into the terminal again and type this...

elm make LED_Numbers.elm --output LED_Numbers.html

Open the file in your browser and run it...



Hope you like it -;)

Greetings,

Blag.
Development Culture.

martes, 10 de noviembre de 2015

LED is my new Hello World - Kotlin Time

Yep...I'm aware that there must be a lot of more efficient and short ways to do this...but then again...my purpose with this is to have the same base code for all the programming languages that I learn...

Usually I write this code after a couple of days of learning the language...so in no way I can be aware of all the crazy and optimized features...

This code is just for fun and for me...a really good way to introduce the syntax of a language in a very friendly way...

So...here's is Kotlin -:)

LEDNumbers.kt
package LEDNumbers

fun main(args: Array<String>) {
 if (args.size == 0) {
      println("Please provide a number...")
      return
    }
    val NumList:List<String> = args[0].split("")
 val Leds = mapOf("0" to listOf(" _  ", "| | ", "|_| "), 
                  "1" to listOf("  ", "| ", "| "),
                  "2" to listOf(" _  "," _| ","|_  "),
                  "3" to listOf("_  ","_| ","_| "),
                  "4" to listOf("    ","|_| ","  | "),
                  "5" to listOf(" _  ","|_  "," _| "),
                  "6" to listOf(" _  ","|_  ","|_| "),
                  "7" to listOf("_   "," |  "," |  "),
                  "8" to listOf(" _  ","|_| ","|_| "),
                  "9" to listOf(" _  ","|_| "," _| "))
 for(i in 0..2){
  for(j in 1..NumList.size - 2){
   print(Leds[NumList[j]]!![i])
  }
  print("\n")
 }                 
}

The result will like this -;)


Greetings,

Blag.
Development Culture.

viernes, 2 de octubre de 2015

LED is my new Hello World - Ada Time

Alright...this one took me a long time...but finally compiled and worked -:)

led_numbers.adb
with Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

procedure LED_Numbers is
 package IO renames Ada.Text_IO;
 package Number_IO is new Ada.Text_IO.Integer_IO (Integer);
 type Array_Row is array (1 .. 3) of Unbounded_String;
 type Array_Of_Array_Type is array (1 .. 10) of Array_Row;
 Array_Of_Arrays : Array_Of_Array_Type;
 s1 : Unbounded_String;
 s2 : Character;
 line1 : Unbounded_String;
 line2 : Unbounded_String;
 line3 : Unbounded_String;
 num : Integer;
 len : Natural;
begin 
 Array_Of_Arrays (1) (1) := s1 & " _  ";Array_Of_Arrays (1) (2) := S1 & "| | ";
        Array_Of_Arrays (1) (3) := S1 & "|_| ";Array_Of_Arrays (2) (1) := s1 & "  ";
        Array_Of_Arrays (2) (2) := S1 & "| ";Array_Of_Arrays (2) (3) := S1 & "| ";
 Array_Of_Arrays (3) (1) := s1 & " _  ";Array_Of_Arrays (3) (2) := S1 & " _| ";
        Array_Of_Arrays (3) (3) := S1 & "|_  ";Array_Of_Arrays (4) (1) := s1 & "_  ";
        Array_Of_Arrays (4) (2) := S1 & "_| ";Array_Of_Arrays (4) (3) := S1 & "_| ";
 Array_Of_Arrays (5) (1) := s1 & "    ";Array_Of_Arrays (5) (2) := S1 & "|_| ";
        Array_Of_Arrays (5) (3) := S1 & "  | ";Array_Of_Arrays (6) (1) := s1 & " _  ";
        Array_Of_Arrays (6) (2) := S1 & "|_  ";Array_Of_Arrays (6) (3) := S1 & " _| ";
 Array_Of_Arrays (7) (1) := s1 & " _  ";Array_Of_Arrays (7) (2) := S1 & "|_  ";
        Array_Of_Arrays (7) (3) := S1 & "|_| ";Array_Of_Arrays (8) (1) := s1 & "_   ";
        Array_Of_Arrays (8) (2) := S1 & " |  ";Array_Of_Arrays (8) (3) := S1 & " |  ";
 Array_Of_Arrays (9) (1) := s1 & " _  ";Array_Of_Arrays (9) (2) := S1 & "|_| ";
        Array_Of_Arrays (9) (3) := S1 & "|_| ";Array_Of_Arrays (10) (1) := s1 & " _  ";
        Array_Of_Arrays (10) (2) := S1 & "|_| ";Array_Of_Arrays (10) (3) := S1 & " _| ";
 IO.Put("Enter a number: ");
 Number_IO.Get(num);
 s1 := s1 & Integer'Image(num);
 len := Length(s1);
 for i in Integer range 2..len loop
  s2 := Element(s1,i);
  num := Character'Pos(s2) - 48;
  if num < 10 then
   num := num + 1;
  end if;
  line1 := line1 & Array_Of_Arrays (num) (1);
  line2 := line2 & Array_Of_Arrays (num) (2);
  line3 := line3 & Array_Of_Arrays (num) (3);
 end loop;
 IO.Put(To_String(line1));
 IO.Put_Line("");
 IO.Put(To_String(line2));
 IO.Put_Line("");
 IO.Put(To_String(line3));
end LED_Numbers;

Now...the screenshots...



Greetings,

Blag.
Development Culture.

viernes, 18 de septiembre de 2015

LED is my new Hello World - Falcon Time

Following my tradition...here's my LED Numbers app using Falcon...I used my Julia code as a base start...and I gotta say...the code is pretty much the same...which is nice for now...but I hope to find some hidden and weird features as I learn more about Falcon -;)

Here's the source code...

LED_Numbers.fal
leds =  ["0" => [" _  ","| | ","|_| "],
         "1" => ["  ","| ","| "],
         "2" => [" _  "," _| ","|_  "],
         "3" => ["_  ","_| ","_| "],
         "4" => ["    ","|_| ","  | "],
         "5" => [" _  ","|_  "," _| "],
         "6" => [" _  ","|_  ","|_| "],
         "7" => ["_   "," |  "," |  "],
         "8" => [" _  ","|_| ","|_| "],
         "9" => [" _  ","|_| "," _| "]]
   
print("Enter a number: ")
number = input()
len = number.len()
for i in [0:3]
 for j in [0:len]
  print(leds[number[j]][i])
 end
 printl("")
end

And of course...here are the pictures...



Greetings,

Blag.
Development Culture.

lunes, 10 de agosto de 2015

LED is my new Hello World - Mercury Time

This one was a little bit tricky and longer than I expected...it also...forced me to spend a lot of time reading the documentation as there aren't many tutorials or books about Mercury...

Anyway...in the end it's working fine, so I'm quite happy -:)

Mercury has a lot of pretty convenient functions...you just need to find them out -;)

led_numbers.m
:- module led_numbers.
:- interface.
:- import_module io.

:- pred main(io::di, io::uo) is det.

:- implementation.
:- import_module list, string, int, array, char.

:- pred get_leds(list.list(character)::in, list.list(character)::in, 
                 int::in, list.list(string)::out ) is det.
 
get_leds(LDIGITS, LDIGITS_AUX, N, RESPONSE) :-
 (
  LEDS = array([array([" _  ","| | ","|_| "]),
                array(["  ","| ","| "]),
                array([" _  "," _| ","|_  "]),
                array(["_  ","_| ","_| "]),
                array(["    ","|_| ","  | "]),
                array([" _  ","|_  "," _| "]),
                array([" _  ","|_  ","|_| "]),
                array(["_   "," |  "," |  "]),
                array([" _  ","|_| ","|_| "]),
                array([" _  ","|_| "," _| "])
               ]),
  list.length(LDIGITS,LEN),
  ( if LEN > 0 then
   HEAD = det_head(LDIGITS),
   TAIL = det_tail(LDIGITS),
   char.to_int(HEAD, HEAD_I:int),
   HEAD_N:int = HEAD_I - 48,
   LINE = elem(HEAD_N, LEDS),
   SUB_LINE = elem(N, LINE),
   get_leds(TAIL, LDIGITS_AUX, N, RESULT),
   RESPONSE = [SUB_LINE] ++ RESULT
    else if N < 2 then
   get_leds(LDIGITS_AUX, LDIGITS_AUX, N+1, RESULT),
   RESPONSE = ["\n"]  ++ RESULT
    else if N = 2 then
   RESPONSE = ["\n"]
    else
   RESPONSE = [] )
 ). 
 
main(!IO) :-
 io.write_string("Enter a number: ",!IO),
 io.read_line_as_string(Result, !IO),
 ( if
   Result = ok(String),
   NUM = string.strip(String),
   to_char_list(NUM,LDIGITS),
   get_leds(LDIGITS, LDIGITS, 0, RESPONSE)
  then
   io.write_string(string.join_list("", RESPONSE), !IO)
  else
   io.write_string("Not a number...",!IO)
 ).


Anyway...here are the screens -;)



Hope you like it -;)

Greetings,

Blag.
Development Culture.

viernes, 31 de julio de 2015

LED is my new Hello World - Zonnon Time

You knew this was coming...don't you? -;)

So here...I took another approach because Zonnon doesn't have a built-in split command...and also because I'm not sure how to create an array of arrays and because I couldn't make my procedure return an array and then assign it to another array -:(

Still...I'm quite happy with the result -:D

LED_Numbers.znn
module LED_Numbers;

var num, text, concat:string;
var leds:array 10 of string;
var led:array 3 of string;
var i, j, z, len, lenled:integer; 

begin
 leds[0]:=" _  ,| | ,|_| ,";
 leds[1]:="  ,| ,| ,";
 leds[2]:=" _  , _| ,|_  ,";
 leds[3]:="_  ,_| ,_| ,";
 leds[4]:="    ,|_| ,  | ,";
 leds[5]:=" _  ,|_  , _| ,";
 leds[6]:=" _  ,|_  ,|_| ,";
 leds[7]:="_   , |  , |  ,";
 leds[8]:=" _  ,|_| ,|_| ,";
 leds[9]:=" _  ,|_| , _| ,";
 write("Enter a number: ");readln(num);
 len:=num.Length;
 for j:=0 to len - 1 do
  i:=0;
  text:=leds[integer(num[j])-48];
  lenled:=text.Length;
  for z:= 0 to lenled - 1 do
   if text[z] # "," then
    concat:= concat + string(text[z]);
   else
    led[i]:=led[i] + concat;
    concat:="";
    i:= i + 1;
   end;
  end;
 end; 
 writeln(led[0]);
 writeln(led[1]);
 writeln(led[2]);
end LED_Numbers.

Anyway...here are the pictures -;)



Greetings,

Blag.
Development Culture.

jueves, 23 de julio de 2015

LED is my new Hello World - Forth Time

Just as you expected...it's time again for my LED Numbers application...this time...using Forth -:)

The code is a little bit longer that I would have expected...but I guess it's either because I'm still a Forth newbie...or because Forth being a stack based programming language has it's own way of dealing with this kind of application...

Here's the source code...enjoy! -;)

LED_Numbers.fth
VARIABLE picky

: COLLECT_PICKY DUP 1- picky ! ;

: NPICK COLLECT_PICKY 2 * 0 ?DO picky @ PICK LOOP ;

: SPLIT BEGIN DUP WHILE 10 /MOD REPEAT DROP DEPTH ;

: First_Line
  CASE
  0 OF SPACE ." _  " ENDOF 1 OF SPACE SPACE ENDOF
  2 OF SPACE ." _  " ENDOF 3 OF ." _  " ENDOF
  4 OF SPACE SPACE SPACE SPACE ENDOF 5 OF SPACE ." _  " ENDOF
  6 OF SPACE ." _  "ENDOF 7 OF ." _   "  ENDOF 
  8 OF SPACE ." _  " ENDOF 9 OF SPACE ." _  " ENDOF
  ENDCASE ;
  
: Second_Line
  CASE
  0 OF ." | | " ENDOF 1 OF ." | " ENDOF
  2 OF SPACE ." _| " ENDOF 3 OF ." _| " ENDOF
  4 OF ." |_| " ENDOF 5 OF ." |_  " ENDOF
  6 OF ." |_  "ENDOF 7 OF SPACE ." |  "  ENDOF 
  8 OF ." |_| " ENDOF 9 OF ." |_| " ENDOF
  ENDCASE ;
  
: Third_Line
  CASE
  0 OF ." |_| " ENDOF 1 OF ." | " ENDOF
  2 OF ." |_  " ENDOF 3 OF ." _| " ENDOF
  4 OF SPACE SPACE ." | " ENDOF 5 OF SPACE ." _| " ENDOF
  6 OF ." |_| "ENDOF 7 OF SPACE ." |  "  ENDOF 
  8 OF ." |_| " ENDOF 9 OF SPACE ." _| " ENDOF
  ENDCASE ;

: LED 
  SPLIT
  NPICK
  DEPTH 3 / 0 ?DO First_Line LOOP CR
  DEPTH 2 / 0 ?DO Second_Line LOOP CR
  DEPTH 0 ?DO Third_Line LOOP ;

And of course...you want to see an image...so here it goes -;)


Hope you like it... -:D

Greetings,

Blag.
Development Culture.

jueves, 18 de junio de 2015

LED is my new Hello World - Lua Time

Getting on with my tradition of building an LED application for each and every new programming language that I learn...it's time for Lua -;)

LedNumbers.lua
local function split(s,delim)
 local result = {}
 for match in (s..delim):gmatch("(.-)"..delim) do
  table.insert(result,match)
 end
 return result
end

leds = {[0] = " _  ,| | ,|_| ",
        [1] = "  ,| ,| ",
        [2] = " _  , _| ,|_  ",
        [3] = "_  ,_| ,_| ",
        [4] = "    ,|_| ,  | ",
        [5] = " _  ,|_  , _| ",
        [6] = " _  ,|_  ,|_| ",
        [7] = "_   , |  , |  ",
        [8] = " _  ,|_| ,|_| ",
        [9] = " _  ,|_| , _| "}

io.write("Enter a number: ")
num = io.read()
for i = 1,3 do
 for j = 1, #num do
  line=split(leds[tonumber(string.sub(num,j,j))],",")
  io.write(line[i])
 end
 print("")
end

What you can see right away...and that's something that really surprised me...is that Lua doesn't provide a "split" or "explode" command out of the box, so you need to make it yourself...actually...the same holds true for Haskell...but for me...functional languages are on another league...and anyway...Haskell's implementation of the split function is way shorter...

Here's the result...


Greetings,

Blag.
Development Culture.

viernes, 12 de junio de 2015

LED is my new Hello World - OpenEuphoria Time

I started using OpenEuphoria a long time ago...when it was called RapidEuphoria...but...I stopped using as I started learning other languages...

Yesterday...all of a sudden...I realized that it is indeed a really cool language, so I wanted to refresh myself and what better than building my LED example one more time -;)

So...here it is -:)

LedNumbers.ex
include get.e
include std/map.e
include std/convert.e
include std/sequence.e

sequence num
sequence snum
object onum
atom anum
map leds = new()
   put(leds, 0, {" _  ","| | ","|_| "})
   put(leds, 1, {"  ","| ","| "})
   put(leds, 2, {" _  "," _| ","|_  "})
   put(leds, 3, {"_  ","_| ","_| "})
   put(leds, 4, {"    ","|_| ","  | "})
   put(leds, 5, {" _  ","|_  "," _| "})
   put(leds, 6, {" _  ","|_  ","|_| "})
   put(leds, 7, {"_   "," |  "," |  "})
   put(leds, 8, {" _  ","|_| ","|_| "})
   put(leds, 9, {" _  ","|_| "," _| "})
   
num = prompt_string("Enter a number: ")
snum = breakup(num,1)
for i = 1 to 3 do
        for j = 1 to length(num) do
            anum = to_number(snum[j])
            onum = map:get(leds,anum)
            puts(1,onum[i])
        end for
        puts(1,"\n")
end for
puts(1,"\n")

Of course...you want to see it in action -:D


If you haven't already...give OpenEuphoria a try...it's really fun -;)

Greetings,

Blag.
Development Culture.

jueves, 11 de junio de 2015

LED is my new Hello World - Pony Time

As you know...I'm always in a constant search for new, weird and cool programming languages...well...this time...the language came to me -;)

I was contacted by @scblessing letting me know about a programming language his company is working on...Pony.

So...what's exactly is Pony? Pony is an object-oriented, actor-model, capabilities-secure, high performance programming language.

Think about it as a nice mix of C++ and Erlang -;)

The documentation is not yet complete...but it's a good starting point...and also...it provides a Sandbox where you can read the source of Pony libraries and execute some fine examples...

So of course...I couldn't be happy with reading and playing around with it...I needed to code my LED as I do with all other programming languages...so here it is -;)

main.pony
actor Main
 var _env: Env
 
 new create(env: Env) =>
  _env = env
  let leds: Array[Array[String]] = [[" _  ","| | ","|_| "],
                                    ["  ","| ","| "],
                                    [" _  "," _| ","|_  "],
                                    ["_  ","_| ","_| "],
                                    ["    ","|_| ","  | "],
                                    [" _  ","|_  "," _| "],
                                    [" _  ","|_  ","|_| "],
                                    ["_   "," |  "," |  "],
                                    [" _  ","|_| ","|_| "],
                                    [" _  ","|_| "," _| "]]

  var num: String = try env.args(1) else "" end
  var i: I64 = 0
  var j: I64 = 0
  var line: String = ""
  while i < 3 do
   while j < num.size().string().i64() do
    try line = line.insert(line.size().string().i64(),
             leds(num.substring(j,j).u64())(i.string().u64())) else "" end
    j = j + 1
   end
   i = i + 1
   j = 0
   _env.out.print(line)
   line = ""
  end
  _env.out.print("")

And here...you can see it in action -:D



My thoughts are that Pony while still a young language, has a lot of potential and my quick experience with it was nothing but fun and exciting...I will keep a close look at it -;)

Greetings,

Blag.
Development Culture.

miércoles, 1 de abril de 2015

LED is my new Hello World - Clojure Time

The more I learn Clojure...the more I like it...and I'm liking it so much that I couldn't help myself and start working on my beloved "LED_Numbers" application...

After making the Fibonnaci Generator app work...this one wasn't as hard as I expected...actually I think I'm slowly getting used to Clojure...which is always nice when learning a new language -;)

Here's the source code...

LED_Numbers.clj
(def leds {"0" (list " _  " "| | " "|_| ") "1" (list "  " "| " "| ")
           "2" (list " _  " " _| " "|_  ") "3" (list "_  " "_| " "_| ")
           "4" (list "    " "|_| " "  | ") "5" (list " _  " "|_  " " _| ")
           "6" (list " _  " "|_  " "|_| ") "7" (list "_   " " |  " " |  ")
           "8" (list " _  " "|_| " "|_| ") "9" (list " _  " "|_| " " _| ")})

(defn toList [number]
 (map str(seq(str number))))

(defn get_led [x n num]
 (cond 
  (> (count x) 0)
   (concat (nth (get leds (first x)) n) (get_led (rest x) n num))
  (and (= (count x) 0) (< n 2))
   (concat "" "\n" (get_led (toList num) (+ 1 n) num))
  (and (= (count x) 0) (= n 2))
   (concat "" "\n")))

(defn showLED [num]
 (do (print (apply str (get_led (toList num) 0 num))))(symbol ""))

Wanna see it in action? Of course you want to -:)


Well...let's go back and keep learning -:D

Greetings,

Blag.
Development Culture.

lunes, 2 de marzo de 2015

LED is my new Hello World - OCaml Time

Learning OCaml is not easy...I have to admit that...but I'm moving forward -;) So, here's my take on LED Numbers...

LED_Numbers.ml
open Core.Std

let get_leds number =
let leds = [0, [" _  ";"| | ";"|_| "];
            1, ["  ";"| ";"| "];
            2, [" _  ";" _| ";"|_  "];
            3, ["_  ";"_| ";"_| "];
            4, ["    ";"|_| ";"  | "];
            5, [" _  ";"|_  ";" _| "];
            6, [" _  ";"|_  ";"|_| "];
            7, ["_   ";" |  ";" |  "];
            8, [" _  ";"|_| ";"|_| "];
            9, [" _  ";"|_| ";" _| "]] in
 for i = 0 to 2 do
  for j = 0 to String.length(number) - 1 do
   let line = List.Assoc.find_exn leds 
        (int_of_string(Char.to_string(number.[j]))) in
   printf "%s" (List.nth_exn line i)
  done;
  print_string "\n"
 done
 
let () =
 print_string "Enter a number: "
 let num = read_line() in
 get_leds num

The funny thing is that at first I tried to translate my previous Julia and Go codes...but then I remembered that even when OCaml can be made into some sort of an imperative language...it's actually Functional in its core...so I said...Ok...I need to reuse my Haskell code...but somehow I start thinking about a new whole different way of doing it...and I think I came out with a more cleaner and well done version...which is something that only learning a new programming language can give you...a new way of thinking about old problems -;)

Here are the screenshots as always -:)



Greetings,

Blag.
Development Culture.


miércoles, 29 de octubre de 2014

LED is my new Hello World - Go Time

As I keep learning Go, I'm learning more commands...so...as usual...here's my take on LED Numbers...maybe not the best code ever...but it works -:)

LED.go
package main

import ( "fmt"
   "strconv" 
   "strings" )

func main() {
 fmt.Print("Enter a number: ")
 var num int
 var list []string
 var line1, line2, line3 string
 fmt.Scanf("%d", &num)
 numList := strings.Split(strconv.Itoa(num), "")
 romans := map[string]string {
  "0" : " _  ,| | ,|_| ",
  "1" : "  ,| ,| ",
  "2" : " _  , _| ,|_  ",
  "3" : "_  ,_| ,_| ",
  "4" : "    ,|_| ,  | ",
  "5" : " _  ,|_  , _| ",
  "6" : " _  ,|_  ,|_| ",
  "7" : "_   , |  , |  ",
  "8" : " _  ,|_| ,|_| ",
  "9" : " _  ,|_| , _| ",
 }
 for _, value := range numList {
  list = strings.Split(romans[value],",")
  line1 += list[0]
  line2 += list[1]
  line3 += list[2]
 }
 fmt.Println(line1)
 fmt.Println(line2)
 fmt.Println(line3)
}

Here are the screenshots...



Greetings,

Blag.
Development Culture.

miércoles, 15 de octubre de 2014

LED is my new Hello World - Racket Time

If you thought I was going to miss the change to build an LED Number Generator in Racket...you got it all wrong -;)

I have to admit...it wasn't easy...Racket is nice but weird...takes some time to get used to it...but when things work...they just work -;)

Here's the source code...

LED.rkt
#lang racket
(define (showLED num)
  (display(get_led(toList num num 0 1) 1 num)))

(define (toList num num_o start end)
  (cond [(number? num) (toList(number->string num) (number->string num_o) start end)]
        [(and (string? num) (> (string-length num_o) 1)) (append (cons(substring num start end) '())
                                                                (toList num (substring num_o 1 (string-length num_o)) 
                                                                        (add1 start)(add1 end)))]
        [(and (string? num) (= (string-length num_o) 1)) (append (cons(substring num_o 0 1) '()))]))

(define (get_led x n num)
  (cond [(> (length x) 0) (append (cons(make_led_digit (string->number(first x)) n) '()) (get_led(rest x) n num))]
        [(and (= (length x) 0) (< n 3)) (append '("\n") (get_led(toList num num 0 1) (add1 n) num) )]
        [(and (= (length x) 0) (>= n 3)) append '()]))

(define (make_led_digit a b)
  (cond [(and (= a 0)(= b 1)) " _  "]
        [(and (= a 0)(= b 2)) "| | "]
        [(and (= a 0)(= b 3)) "|_| "]
        [(and (= a 1)(= b 1)) "  "]
        [(and (= a 1)(= b 2)) "| "]
        [(and (= a 1)(= b 3)) "| "]
        [(and (= a 2)(= b 1)) " _  "]
        [(and (= a 2)(= b 2)) " _| "]
        [(and (= a 2)(= b 3)) "|_  "]
        [(and (= a 3)(= b 1)) "_  "]
        [(and (= a 3)(= b 2)) "_| "]
        [(and (= a 3)(= b 3)) "_| "]
        [(and (= a 4)(= b 1)) "    "]
        [(and (= a 4)(= b 2)) "|_| "]
        [(and (= a 4)(= b 3)) "  | "]
        [(and (= a 5)(= b 1)) " _  "]
        [(and (= a 5)(= b 2)) "|_  "]
        [(and (= a 5)(= b 3)) " _| "]        
        [(and (= a 6)(= b 1)) " _  "]
        [(and (= a 6)(= b 2)) "|_  "]
        [(and (= a 6)(= b 3)) "|_| "]
        [(and (= a 7)(= b 1)) "_   "]
        [(and (= a 7)(= b 2)) " |  "]
        [(and (= a 7)(= b 3)) " |  "]
        [(and (= a 8)(= b 1)) " _  "]
        [(and (= a 8)(= b 2)) "|_| "]
        [(and (= a 8)(= b 3)) "|_| "]
        [(and (= a 9)(= b 1)) " _  "]
        [(and (= a 9)(= b 2)) "|_| "]
        [(and (= a 9)(= b 3)) " _| "]))

Here's how it looks like when we run it...


Well...gotta keep learning...so...see you soon -;)

Greetings,

Blag.
Development Culture.

viernes, 29 de agosto de 2014

LED is my new Hello World - ABAP Time

It's been a long time since I did any ABAP development...so just for the good all times I decided to build the LED Number application on ABAP as well... -;)

I'm might be a little bit rusty...but I think I still remember most of the all tricks -:P

This goes out to all my ABAP friends...there are plenty -:)

ZLED
REPORT  zled.

TYPES: BEGIN OF ty_lines,
       line(1) TYPE c,
       index(1) TYPE c,
       map TYPE string,
       END OF ty_lines.

DATA: s_number TYPE string,
      counter TYPE i,
      num_counter TYPE i,
      line1 TYPE string,
      line2 TYPE string,
      line3 TYPE string.

DATA: t_lines TYPE STANDARD TABLE OF ty_lines.
FIELD-SYMBOLS: <fs_lines> LIKE LINE OF t_lines.

SELECTION-SCREEN BEGIN OF BLOCK params.
PARAMETERS: p_number TYPE i.
SELECTION-SCREEN END OF BLOCK params.

START-OF-SELECTION.
  PERFORM load_data.
  num_counter = 0.
  s_number = p_number.
  counter = strlen( s_number ) - 1.
  DO counter TIMES.
    READ TABLE t_lines ASSIGNING <fs_lines>
    WITH KEY line = 1
             index = s_number+num_counter(1).
    CONCATENATE line1 <fs_lines>-map INTO line1 SEPARATED BY space.
    READ TABLE t_lines ASSIGNING <fs_lines>
    WITH KEY line = 2
             index = s_number+num_counter(1).
    CONCATENATE line2 <fs_lines>-map INTO line2 SEPARATED BY space.
    READ TABLE t_lines ASSIGNING <fs_lines>
    WITH KEY line = 3
             index = s_number+num_counter(1).
    CONCATENATE line3 <fs_lines>-map INTO line3 SEPARATED BY space.
    num_counter = num_counter + 1.
  ENDDO.
  REPLACE ALL OCCURRENCES OF '%' IN line1 WITH ` ` IN CHARACTER MODE.
  REPLACE ALL OCCURRENCES OF '%' IN line2 WITH ` ` IN CHARACTER MODE.
  REPLACE ALL OCCURRENCES OF '%' IN line3 WITH ` ` IN CHARACTER MODE.
  WRITE:/ line1.
  WRITE:/ line2.
  WRITE:/ line3.

*&---------------------------------------------------------------------*
*&      Form  LOAD_DATA
*&---------------------------------------------------------------------*
FORM load_data.

  PERFORM add_lines USING '1' '0' '%_%%'.
  PERFORM add_lines USING '2' '0' '| |%'.
  PERFORM add_lines USING '3' '0' '|_|%'.
  PERFORM add_lines USING '1' '1' '%%'.
  PERFORM add_lines USING '2' '1' '|%'.
  PERFORM add_lines USING '3' '1' '|%'.
  PERFORM add_lines USING '1' '2' '%_%%'.
  PERFORM add_lines USING '2' '2' ' _|%'.
  PERFORM add_lines USING '3' '2' '|_%%'.
  PERFORM add_lines USING '1' '3' '_%%'.
  PERFORM add_lines USING '2' '3' '_|%'.
  PERFORM add_lines USING '3' '3' '_|%'.
  PERFORM add_lines USING '1' '4' '%%%%'.
  PERFORM add_lines USING '2' '4' '|_|%'.
  PERFORM add_lines USING '3' '4' '  |%'.
  PERFORM add_lines USING '1' '5' '%_%%'.
  PERFORM add_lines USING '2' '5' '|_ %'.
  PERFORM add_lines USING '3' '5' ' _|%'.
  PERFORM add_lines USING '1' '6' '%_%%'.
  PERFORM add_lines USING '2' '6' '|_%%'.
  PERFORM add_lines USING '3' '6' '|_|%'.
  PERFORM add_lines USING '1' '7' '_%%'.
  PERFORM add_lines USING '2' '7' '%|%'.
  PERFORM add_lines USING '3' '7' '%|%'.
  PERFORM add_lines USING '1' '8' '%_%%'.
  PERFORM add_lines USING '2' '8' '|_|%'.
  PERFORM add_lines USING '3' '8' '|_|%'.
  PERFORM add_lines USING '1' '9' '%_%%'.
  PERFORM add_lines USING '2' '9' '|_|%'.
  PERFORM add_lines USING '3' '9' ' _|%'.

ENDFORM.                    " LOAD_DATA

*&---------------------------------------------------------------------*
*&      Form  add_lines
*&---------------------------------------------------------------------*
FORM add_lines USING p_line p_index p_map.
  APPEND INITIAL LINE TO t_lines ASSIGNING <fs_lines>.
  <fs_lines>-line = p_line.
  <fs_lines>-index = p_index.
  <fs_lines>-map = p_map.
ENDFORM.                    "add_lines

Pics or it didn't happen -:)





Greetings,

Blag.
Development Culture.



LED is my new Hello World - R Time

Yep...as I have said many times before, the LED Number application has become my new Hello World...whenever I learn a new programming language I try to build this, because it compromises several language constructs and makes you learn more by trying to figure out how to build it again...

Yesterday I blog about how to build it using Haskell...and...I'm still learning Haskell...so I thought..."It's cool that I'm using this for my new programming languages...but what about the old ones?"

I suddenly realized that I had never wrote this using R...and that's really sad...I love R -:)

Anyway...here it goes -;)

LED.R
line<-c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3)
index<-c(0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9)
map<-c("  _ ","| | ","|_| ","  ","| ","| ","  _ "," _| ","|_  ",
       " _","_| ","_| ","    ","|_| ","  | ","  _ ","|_  "," _| ",
       "  _ ","|_  ","|_| "," _  "," |  "," |  ","  _ ","|_| ","|_| ",
       "  _ ","|_| "," _| ")

Lines<-cbind(line,index,map)
DF_Lines<-data.frame(Lines,stringsAsFactors=F)

line1<-""
line2<-""
line3<-""

p_number<-readline("Enter a number?: ")
l_number<-strsplit(p_number,"")
for(i in 1:nchar(p_number)){
  num_map<-DF_Lines[DF_Lines$index == as.numeric(l_number[[1]][i]),]
  line1<-paste(line1,num_map$map[1])
  line2<-paste(line2,num_map$map[2])
  line3<-paste(line3,num_map$map[3])
}
lines = paste(line1,"\n",line2,"\n",line3,"\n")
cat(lines)

Check the picture -;)


Greetings,

Blag.
Development Culture.