Code Monkey home page Code Monkey logo

arraysugar's Introduction

ArraySugar

Sugar for array

How do I get it

  • SPM: .package(url: "https://github.com/eonist/ArraySugar.git", .branch("master"))
  • Manual Open .xcodeproj

Examples:

BinaryIndex

let key = 11
var numbers = [9, 15, 91]
let idx = binaryIndex(numbers, key, 0, numbers.count) // 1
if idx >= numbers.count || numbers[idx] != key { numbers.insert(key, at: idx) }
print(numbers) // [9, 11, 15, 91]

BinarySearch

let numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67]
binarySearch(numbers, key: 43, range: 0 ..< numbers.count) // output: 13 which is the index of where the key is

RecursiveFlatmap

let arr: [Any] = [[[1], [2, 3]], [[4, 5], [6]]] // ๐Ÿ‘ˆ 3d array (3 depths deep)
let x2: [Int] = arr.recursiveFlatmap()
Swift.print(x2)//[1,2,3,4,5,6]

Scale array

Swift.print(scaleArr(arr: [0, 1, 1, 0], size: (width: 2, height: 2), scale:2))// [0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0]
Swift.print(scaleArr(arr: [0, 0, 0, 1, 0, 1, 0, 0, 0], size: (width: 3, height: 3), scale: 2)) // [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Todo:

  • Clean up comments
  • Add UnitTest
  • Add CI

arraysugar's People

Contributors

eonist avatar

Stargazers

 avatar  avatar  avatar  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.