Code Monkey home page Code Monkey logo

generic_functions's Introduction

Generic_Functions

/* Generic functions :- Generic code enables you to write flexible, reusable functions and types that can work with any type, subject to requirements that you define. You can write code that avoids duplication and expresses its intent in a clear, abstracted manner. */

let IntArr:[Int] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21] let StrArr:[String] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

// Write a function for finding index in integer type Array.

func findIndexInt(arr:[Int],key:Int) ->Int { for (index,element) in arr.enumerated() { if element == key { return index } } return -1; }

// Write a function for finding index in String type Array.

func findIndexStr(arr:[String],key:String) ->Int { for (index,element) in arr.enumerated() { if element == key { return index } } return -1; }

findIndexInt(arr: IntArr, key: 20) findIndexStr(arr: StrArr, key: "k")

// create a generic function for finding the index in the array. due to this function you dont need to write any other function.this support any type of array.

func getIndexOfItems<T:Comparable>(itemsArr :[T],key:T)-> Int { for (index,items) in itemsArr.enumerated() { if items == key { return index } } return -1; }

getIndexOfItems(itemsArr: StrArr, key: "k") getIndexOfItems(itemsArr: IntArr, key: 20)

generic_functions's People

Contributors

ashum8 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.