Knowledge in Programming

map insert() in C++

#include <bits/stdc++.h> using namespace std; int main()  {     map<int, int> mp;     mp.insert({ 2, 30 });     mp.insert({ 1, 40 });     mp.insert({ 3, 60 });     mp.insert({ 2, 20 });     mp.insert({ 5, 50 });     cout << "\tANYTHING\n";     for (auto itr = mp.begin(); itr != mp.end(); ++itr) {      cout << itr->first<< '\t' << itr->second << '\n';     }     return 0; }

F# - Program Structure

open System let sign num = if num > 0 then "positive" elif num < 0 then "negative" else "zero" let main() = Console.WriteLine("sign 5: {0}", (sign 5)) main()

Strings In Elixir

Strings in Elixir are inserted between double quotes, and they are encoded in UTF-8. Unlike C and C++ where the default strings are ASCII encoded and only 256 different characters are possible, UTF-8 consists of 66536 code points. This means that UTF-8 encoding consists of those many different possible characters. Since the strings use utf-8, we can also use symbols like: ö, ł, etc. PROGRAM: str = "Hello world" a = "" if String.length(a) === 0 do IO.puts("a is an empty string") end String Interpolation String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. Elixir supports string interpolation, to use a variable in a string, when writing it, wrap it with curly braces and prepend the curly braces with a '#' sign. x = "Apocalypse" y = "X-men #{x}" IO.puts(y)

Protocols in Elixr

Protocols are a mechanism to achieve polymorphism in Elixir. Dispatching on a protocol is available to any data type as long as it implements the protocol. Let us consider an example of using protocols. We used a function called to_string in the previous chapters to convert from other types to the string type. This is actually a protocol. It acts according to the input that is given without producing an error. This might seem like we are discussing pattern matching functions, but as we proceed further, it turns out different. CODE: defprotocol Blank do def blank?(data) end Implementing a Protocol: defprotocol Blank do def blank?(data) end #Implementing the protocol for lists defimpl Blank, for: List do def blank?([]), do: true def blank?(_), do: false end defimpl Blank, for: BitString do def blank?(""), do: true def blank?(_), do: false end defimpl Blank, for: Map do def blank?(map), do: map_size(map) == 0 end IO.puts(Blank.blank? []) IO.puts(Blank.blank? [:true, "Hello"]) IO.puts(Blank.blank? "") IO.puts(Blank.blank? "Hi")

Processes In Elixr

In Elixir, all code runs inside processes. Processes are isolated from each other, run concurrent to one another and communicate via message passing. Elixir’s processes should not be confused with operating system processes. Processes in Elixir are extremely lightweight in terms of memory and CPU (unlike threads in many other programming languages). Because of this, it is not uncommon to have tens or even hundreds of thousands of processes running simultaneously. The Spawn Function The easiest way to create a new process is to use the spawn function. The spawn accepts a function that will be run in the new process. For example − pid = spawn(fn -> 2 * 2 end) Process.alive?(pid) Message Passing : send(self(), {:hello, "Hi people"}) receive do {:hello, msg} -> IO.puts(msg) {:another_case, msg} -> IO.puts("This one won't match!") end

Regex In Elixr

Regex Regexes in Elixir are sigils. We have seen their use in the String chapter. Let us again take an example to see how we can use regex in Elixir. Code: regex = ~r/foo|bar/ IO.puts("foo" =~ regex) IO.puts("baz" =~ regex) Strings The ~s sigil is used to generate strings, like double quotes are. The ~s sigil is useful, for example, when a string contains both double and single quotes − new_string = ~s(this is a string with "double" quotes, not 'single' ones) IO.puts(new_string)

Macros in elixir

Macros are one of the most advanced and powerful features of Elixir. As with all advanced features of any language, macros should be used sparingly. They make it possible to perform powerful code transformations in compilation time. We will now understand what macros are and how to use them in brief. {:sum, [], [1, 2, 3]} The first element is the function name, the second is a keyword list containing metadata and the third is the arguments list. You can get this as the output in iex shell if you write the following − quote do: sum(1, 2, 3) Operators are also represented as such tuples. Variables are also represented using such triplets, except that the last element is an atom, instead of a list. When quoting more complex expressions, we can see that the code is represented in such tuples, which are often nested inside each other in a structure resembling a tree. Many languages would call such representations an Abstract Syntax Tree (AST). Elixir calls these quoted expressions.

Functions in Elixir

A function is a set of statements organized together to perform a specific task. Functions in programming work mostly like function in Math. You give functions some input, they generate output based on the input provided. There are 2 types of functions in Elixir − Anonymous function Functions defined using the fn..end construct are anonymous functions. These functions are sometimes also called as lambdas. They are used by assigning them to variable names. Named function Functions defined using the def keyword are named functions. These are native functions provided in Elixir. Anonymous Functions Just as the name implies, an anonymous function has no name. These are frequently passed to other functions. To define an anonymous function in Elixir, we need the fn and end keywords. Within these, we can define any number of parameters and function bodies separated by ->. For example, Live Demo sum = fn (a, b) -> a + b end IO.puts(sum.(1, 5)) When running above program, is run, it generates the following result − 6 Note that these functions are not called like the named functions. We have a '.' between the function name and its arguments. Using the Capture Operator We can also define these functions using the capture operator. This is an easier method to create functions. We will now define the above sum function using the capture operator, sum = &(&1 + &2) IO.puts(sum.(1, 2)) When the above program is run, it generates the following result − 3 In the shorthand version, our parameters are not named but are available to us as &1, &2, &3, and so on. Pattern Matching Functions Pattern matching is not only limited to variables and data structures. We can use pattern matching to make our functions polymorphic. For example, we will declare a function that can either take 1 or 2 inputs (within a tuple) and print them to the console,   handle_result = fn {var1} -> IO.puts("#{var1} found in a tuple!") {var_2, var_3} -> IO.puts("#{var_2} and #{var_3} found!") end handle_result.({"Hey people"}) handle_result.({"Hello", "World"})

Typespecs in Elixir

Elixir is a dynamically typed language, so all types in Elixir are inferred by the runtime. Nonetheless, Elixir comes with typespecs, which are a notation used for declaring custom data types and declaring typed function signatures (specifications). Function Specifications(specs) By default, Elixir provides some basic types, such as integer or pid, and also complex types: for example, the round function, which rounds a float to its nearest integer, takes a number as an argument (an integer or a float) and returns an integer. In the related documentation, the round typed signature is written as − round(number) :: integer The above description implies that the function on the left takes as argument what is specified in parenthesis and returns what is on the right of ::, i.e., Integer. Function specs are written with the @spec directive, placed right before the function definition. The round function can be written as − @spec round(number) :: integer def round(number), do: # Function implementation ... Typespecs support complex types as well, for example, if you want to return a list of integers, then you can use [Integer] Custom Types While Elixir provides a lot of useful inbuilt types, it is convenient to define custom types when appropriate. This can be done when defining modules through the @type directive. Let us consider an example to understand the same − defmodule FunnyCalculator do @type number_with_joke :: {number, String.t} @spec add(number, number) :: number_with_joke def add(x, y), do: {x + y, "You need a calculator to do that?"} @spec multiply(number, number) :: number_with_joke def multiply(x, y), do: {x * y, "It is like addition on steroids."} end {result, comment} = FunnyCalculator.add(10, 20) IO.puts(result) IO.puts(comment) When the above program is run, it produces the following result − 30

Matlab code to convert one coordinate system to another.

Matlab is best for simulation purpose. It is used in almost every field of engineering.

Arduino code for bluetooth car.

Arduino is the best platform to perform projects on robotics. It is very good micro controller.

Python Career Path

An comprehensive guide for career options with Python Programming Language. Learn Python Programming now and boost up your career ! For FREE Video Course visit :- https://www.edyoda.com/course/98