Skip to content

Instantly share code, notes, and snippets.

@vibinnair
vibinnair / gist:51e95bdbe180af2d07f3ceca522250d7
Created August 5, 2019 07:51
Functional Swift: Retreive all hashtags from a string
//Ref: https://www.swiftbysundell.com/basics/map-flatmap-and-compactmap
import Foundation
func hashTags(from string: String) -> [String] {
return string.components(separatedBy: .whitespacesAndNewlines)
.filter {string in string.starts(with: "#")}
}
print(hashTags(from: "#WWDC 2019 was #awesome #greatday"))
https://www.hackerrank.com/challenges/fp-filter-positions-in-a-list/problem
import Foundation
func filterOddElements(_ list: [Int]) -> [Int] {
if list.count <= 1 {
return []
}
let elements = list.prefix(2)
https://www.hackerrank.com/challenges/fp-filter-array/problem
import Foundation
func filterList(_ upperLimit: Int, _ list: [Int]) -> [Int] {
if list.count <= 0 {
return []
}
let element = list.first!
https://www.hackerrank.com/challenges/fp-list-replication/problem
import Foundation
func printElements(_ numberOfTimes: Int, _ list: [Int]) -> [Int] {
if list.count <= 0 {
return list
}
https://www.hackerrank.com/challenges/fp-hello-world-n-times/problem
import Foundation
func helloWorld(_ numberOfTimes: Int) {
if numberOfTimes <= 0 {
return
}
print("Hello World")
import Foundation
func add(_ x: Int, _ y: Int) -> Int {
return x + y
}
func square(_ x: Int) -> Int {
return x * x
}
import Foundation
func sumOfElements(_ array: [Int], _ sum: Int) -> Int {
if array.count <= 0 {
return sum
}
let newArray = Array(array.dropFirst(1))
return sumOfElements(newArray, sum + array.first!)
}
ACTION = build
AD_HOC_CODE_SIGNING_ALLOWED = NO
ALTERNATE_GROUP = staff
ALTERNATE_MODE = u+w,go-w,a+rX
ALTERNATE_OWNER = grantdavis
ALWAYS_SEARCH_USER_PATHS = NO
ALWAYS_USE_SEPARATE_HEADERMAPS = YES
APPLE_INTERNAL_DEVELOPER_DIR = /AppleInternal/Developer
APPLE_INTERNAL_DIR = /AppleInternal
APPLE_INTERNAL_DOCUMENTATION_DIR = /AppleInternal/Documentation
@vibinnair
vibinnair / bundle-error-rmagic
Last active December 10, 2015 21:08
bundle-error whle installing rmagic gem
Installing rmagick (2.12.2) with native extensions
Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.
/home/vibin/.rvm/rubies/ruby-1.9.3-p194/bin/ruby extconf.rb
checking for Ruby version >= 1.8.5... yes
extconf.rb:107: Use RbConfig instead of obsolete and deprecated Config.
checking for gcc... yes
checking for Magick-config... no
Can't install RMagick 2.12.2. Can't find Magick-config in /home/vibin/.rvm/gems/ruby-1.9.3-p194/bin:/home/vibin/.rvm/gems/ruby-1.9.3-p194@global/bin:/home/vibin/.rvm/rubies/ruby-1.9.3-p194/bin:/home/vibin/.rvm/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
@vibinnair
vibinnair / migration error for mysql db
Created December 19, 2012 14:34
migration fails for AddColumnPublishedToSurveys migration file in survey-web application.
== AddColumnPublishedToSurveys: migrating ====================================
-- add_column(:surveys, :published, :boolean, {:default=>:false})
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: Invalid default value for 'published': ALTER TABLE `surveys` ADD `published` tinyint(1) DEFAULT 'false'
/home/volition/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `query'
/home/volition/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in `block in execute'
/home/volition/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/volition/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'