Sayfalar

26 Eylül 2014 Cuma

Multi-Threading In Android and Handler Class - I

Multi-Threading In Android and Handler Class - I 

Multi-Threading in Android

When I started research this topic first question about threading is why i need threads? There is always tags like fast,usable,multicore .etc. All of them good tags which i interest in all of type programming language :)With multi-thereading our app's will be more fast and more useable,but WHY ? Java,C or C++ are written in very past , and all of them create for single core processor but today we have a lot of processer even in mobile phones.And today we can use multi-cores with same ways in these languages.In Java multi-threads is a way to use this power.In android we can use regular threads as in Java but we must be consider about Android threads(UI Threads) and some classes.

In android there is a class which responsible to handle  topic about threads,and  it's name is Handler class:).With messages all of the threads comminicate with each other in this class.It is simple to create a handler

Handler handlerObject = new Handler(); 

We use the Runnablers for multi-threadings.They are type of thread whic can execute their body or call another functions etc.They include a abstract method void run() all of the threads must be have this method.

class ClassName implements Runnable 
{
Public void run() 
{
// Body of method
}
}

When we use the Handler in our android project we must be add import android.os.Handler; lines in aour projects.
And in our project we create a handler like on the top.

Handler maygun = new Handler(); 

And then we can create a Runnable

Runnable run = new Runnable() 
{
public void run()
{
// Our functions
  }
};


I will continue with Handler Mechanism and AsyncTask in next blog post.

26 Ağustos 2014 Salı

C++ 14 Announced

https://isocpp.org/blog/2014/08/we-have-cpp14

C++ 14 standard has been announced.C++ 14 have a lot of new capabilities and this capabilities is made C++ more powerful and modern language.Here a blog post for new features about C++14.

Clang 3.4.x and newest version of GCC support C++ 14 standard.

Here some of the features which really affect me  ;

N3532  - Dynamic Arrays 

N3534 - C++ Pipelines

N3554 - A Parallel Algorithms Library for C++

N3559 - Proposal for Generic (Polymorphic) Lambda Expressions


Dynamic Arrays will play huge role for memory management in C++ projects.

C++ Pipelines and Parallel Algorithm library is also very important , because all of the computer architecture in today have multiple core system and we must use all of the power for strong and fast software.

Lambda  came with C++11 but now it have more power in C++.


1 Ağustos 2014 Cuma

Haskell Day6

Basic I/O

Here is the most classic program ever :)

main = do
     print(“Hello Word”)

If we want to get input in Haskell we can use getline function for strings,integers or other basic input data. For example :


main= do
       putStrLn "enter age"
       age <- getLine
       putStrLn("Hello " ++ age ++ " boy")

Let do some fun thing with simple ı/o and some condition situations :)

main= do
          putStrLn "enter age"
          age <- getLine
          if read(age) > 50
                  then do print("You are too old man")
          else
                 do print("Yeah men you have a lot of time for Haskell !")


In line 2 we enter the value of the age.line 3 take the argument and equalize the value of the age variable. In line 4 with read function we can compare a string and a integer because Getline function get string and read function change it.then line in line 4 if do the condition things and “then do” parts is returning part.

29 Temmuz 2014 Salı

Haskell Day5

About Functions

Today I learned about function composition thing in Haskell. Firstly I start with simple composition example. In math we can very easily compose two or more functions .İ.e : there are two functions f(x) and g(x) and the composition function can be writed like t(x) = g o f or h(x) = f o g t(x) and h(x) will different functions. In Haskell we can combine function with using a functions as a parameter like:

f x = x + 2
g x = x * 3
a = f (g 4)
b =g(f 4)
main = do
           print(a)
           print(b)

Program will return 14 and 18 .We must use parentheses otherwise we can use “.”
f ( g x ) = = (f . g) x

Lets start with fundamental thing of structural programming : “decision part”.In Haskell we use If,Then and Else structure for making decision. For example there are three possibility about f(x).

f(x) can be positive or negative or zero so

mysig x =
             if x < 0 – deciding
                    then -1 – if firs condition ok what happened
                    else if x >0 – second condition
                          then 1 – if second condition ok what happened
                          else 0 – what happened if all of condition is wrong
main=print(mysig (0))

it will return 0.Also this function can be write like this

mysig x
         | x < 0 = -1
         | x > 0 = 1
         | otherwise = 0


It is so similar to functions in math.  

27 Temmuz 2014 Pazar

Haskell Day4

List and Tuples

Haskell have two important data storage system such as lists and tuples. They are very important thing for Haskell like functions. Lists are implemented by “[ ]” but Tuples with “( )”.The differences between lists and tuples are :
#Tuples size is constant but list are flexible so if we know size of our data we should use tuples for better performance.
#Tuples can be storage different type of data like a Truth value and a string can be in same tuples but list not allow this and return a error which about data types for example a name and a telephone number can be use in a same tuple but they cannot be in a list.

In list and tuples we define elements separating with comas.

list_ex = [1,2,3]
list1_ex = [“ahmet”,”mehmet”,”as”]
tuples_ex = (“john,”,1,True)
main = do
     print(list_ex)
     print(list1_ex)
     print(tuples_ex)


Some mistake coding example

list_ex = [1,”2”,3] – error 1 is a integer but “2” is a char so types are different

we can add elements into a list with using “:”.For example;

list_ex[1,2,3]
main = print(0:list_ex)


program will return 0,1,2,3  

26 Temmuz 2014 Cumartesi

Haskell Day 3

Learning Haskell Day3

Today i discover library of haskell.It is very easy to import a library on our codes. "import" keywords handle everything :)

For example i added System.Info library for using some ready function which implemented before.With this code we can learn system's info about OS or compiler etc.


import System.Info

main=do
print os
print compilerName
print compilerVersion

This code will return for me "linux"  "ghc" and "version versionbranch[7,6]"

Here some basic libraries for Haskell these libraries have some ready functions and data types for using if you interested link look down :)

http://www.haskell.org/ghc/docs/6.12-latest/html/libraries/base-4.2.0.1/

25 Temmuz 2014 Cuma

Haskell Day2


Learning Haskell Day2

         Today I learned about truth values and basic types. Truth values similar to other high-level programming languages. There are two variables such as “True” and “False”. If a condition is not right it will be false otherwise true. For example this program print False

sq x = x * x
main=print(sq 2 ==5 )

Or this one will return true

vol x y z = x*y*z
main=print(vol 3 4 5 == 60).

Of course we can do some boolean operation like “and” and “or”.For and we use && and if one of the condition is false it will be false.For or we use || it is enough to being true if one of them is true.For example:

main=do
      print(3==3 && 4==2)
      print(3==3 || 4==2)
      print((==) 3 5 )

the output like
false true false

first two line is seems normal but third condition is different. The == operator can bu used like a prefix operator even it take two arguments.

Types in Haskell very similar to other programming languages char strings... . If you want to learn a type of a variables on ghci you can use :type ot :t for example :t True will be return boolean.