Showing posts with label functional. Show all posts
Showing posts with label functional. Show all posts

Friday, September 11, 2015

Swift 2.0 Strings


After functional coding I feel the biggest change from Obj-C world to Swift was in support of Unicode string.

Then again the huge change from Swift 1.2 to Swift 2.0 might be in String.
In someway its a dramatic change !
As stated in Swift Blog

Swift provides a performant, Unicode-compliant string implementation as part of its Standard Library. In Swift 2, theString type no longer conforms to the CollectionType protocol, where String was previously a collection of Character values, similar to an array. Now, String provides a characters property that exposes a character collection view.


var letters: [Character] = ["c", "a", "f", "e"]
var string: String = String(letters)

print(letters.count) // 4
print(string) // cafe
print(string.characters.count) // 4


let acuteAccent: Character = "\u{0301}" // ´ COMBINING ACUTE ACCENT' (U+0301)

string.append(acuteAccent)
print(string.characters.count) // 4
print(string.characters.last!) // é



https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html


NOTE
Extended grapheme clusters can be composed of one or more Unicode scalars. This means that different characters—and different representations of the same character—can require different amounts of memory to store. Because of this, characters in Swift do not each take up the same amount of memory within a string’s representation. As a result, the number of characters in a string cannot be calculated without iterating through the string to determine its extended grapheme cluster boundaries. If you are working with particularly long string values, be aware that the count(_:) function must iterate over the Unicode scalars in the entire string in order to calculate an accurate character count for that string.


The character count returned by the count(_:) function is not always the same as the length property of an NSString that contains the same characters. The length of an NSString is based on the number of 16-bit code units within the string’s UTF-16 rpresentation and not the number of Unicode extended grapheme clusters within the string. To reflect this fact, the length property from NSString is called utf16Count when it is accessed on a Swift String value.

Reference - 

Saturday, September 28, 2013

C++ beginers code for Objective-C Developers using XCode

Problem 1: Using CPP code show the use of If…Then…Else statement-using user input values.

Code

//
//  main.cpp
//  Hello World CPP
//
//  Created by Bishal Ghimire on 9/19/13.
//  Copyright (c) 2013 Bishal Ghimire. All rights reserved.
//

#include <iostream>

using namespace std;


/**
   IF then else function for using user input values
*/

int main(int argc, const char * argv[]) {

    int inputAge;
    std::cout << "Input You Age, !\n";
    cin >> inputAge;
   
   
    if (inputAge >= 18) {
        cout << "You can Vote ! \n";
    }
    else if (inputAge < 18) {
        cout << "You can NOT vote ! \n";
    }
    return 0;
}

Output

$ g++ main.cpp
$ ./a.out
Input You Age, !
 29
You can Vote !

Problem 2: Using functions show how you can swipe variables using call by reference.

Code

/**
    Function to swipe two variables using third temp value
 */
void swap(int *u, int *v) {
    int temp;
    temp = *u;
    *u = *v;
    *v = temp;
    return;
}

int main(int argc, const char * argv[]) {
       
    int firstValue;
    cout<<"Input First Value \n";
    cin >> firstValue;
   
    int secondValue;
    cout<<"Input Second Value \n";
    cin >> secondValue;
   
    swap(firstValue, secondValue);
   
    cout<<"First Value and Second Value after they are Swaped \n";
    cout << "1st = " << firstValue << "\n2nd = " << secondValue;
    cout<<"\n";
    return 0;
}

Output

$ g++ main.cpp
$ ./a.out
Input First Value
12
Input Second Value
34
First Value and Second Value after they are Swaped
1st = 34
2nd = 12

Problem 3: Demonstrate the use of functional overloading in CPP (C++)

 Code

/**
  functionOverLoading.cpp
*/

#include "functionOverLoading.h"

using namespace std;

#define pi 3.141

/**
 Function overloading
 */
class fxn {
public:
    void area(int);              // circle
    void area(int, int);         // rectangle
    void area(int, int, int);    // triangle
};

void fxn::area(int a) {
    cout<<"Area of Circle: "<<pi*a*a;
    cout<<"\n";
}

void fxn::area(int a, int b) {
    cout<<"Area of rectangle: "<<a*b;
    cout<<"\n";
}

void fxn::area(int a, int b, int c) {
    // cout<<"Area of triangle: "<<a*b*c;
    cout<<"\n";
}

typedef enum ChoiceType
{
    choiceCircle    = 1,
    choiceRectangle,
    choiceTriangle,
    choiceExit
} ChoiceType;


int main(int argc, const char * argv[]) {
   
    int choice;
    int radius;
    int length;
    int breath;
    int sideA, sideB, sideC;
    fxn object;
   
    cout<<"\n\t\tFunction Overloading";
    cout<<"\n 1. Area of Circle \n 2. Area of Rectangle \n 3. Area of Triangle \n 4. Exit \n ";
    cout<<"Enter your Choice:";
    cin>>choice;
   
    switch (choice) {

        case choiceCircle:
            cout<<"Enter Radious of the Circle:";
            cin>>radius;
            object.area(radius);
            break;

        case choiceRectangle:
            cout<<"Enter Sides of Rectangle:";
            cin>>length>>breath;
            object.area(length, breath);
            break;

        case choiceTriangle:
            cout<<"Enter Sides of triangle:";
            cin>>sideA>>sideB>>sideC;
            object.area(sideA, sideB, sideC);
            break;

        case choiceExit:
            exit(0);
            break;

        default:
            break;
    }
    return 0;
}

Output 

$g++ functionOverLoading.cpp –o functionOverLoading
$ ./functionOverLoading

Function Overloading
 1. Area of Circle
 2. Area of Rectangle
 3. Area of Triangle
 4. Exit
 Enter your Choice:1
Enter Radious of the Circle:10
Area of Circle: 314.1


$ ./functionOverLoading
            Function Overloading
 1. Area of Circle
 2. Area of Rectangle
 3. Area of Triangle
 4. Exit
 Enter your Choice:2
Enter Sides of Rectangle:10 12
Area of rectangle: 120