Skip to content

Instantly share code, notes, and snippets.

View lnds's full-sized avatar

Eduardo Díaz lnds

View GitHub Profile
@lnds
lnds / ifs.swift
Created January 10, 2016 16:30
If en Swift
if i >= tam {
return nil
} else if c < "0" || c > "9" {
return nil
} else {
...
}
@lnds
lnds / ifs.rust
Last active January 10, 2016 16:44
If en Rust
if !c.is_digit(10) { return Error }
else if i >= tam { return Error }
else { ... }
@lnds
lnds / ifs.scala
Created January 10, 2016 16:43
Ejemplo de If en Scala. Notar la primera condición
def validar(tam:Int, accion:String) : Option[Array[Int]] =
if (accion.exists(!_.isDigit))
None
else {
val num = accion.map(_.toInt - '0'.toInt).distinct
if (num.length == tam && num.length == accion.length)
Some(num.toArray)
else
None
}
@lnds
lnds / validar.scala
Created January 10, 2016 16:52
Otra forma de implementar validar
def validar(tam:Int, accion:String) : Option[Array[Int]] =
if (accion.exists(c => !c.isDigit) None
else {
val num = accion.map(c => c.toInt - '0'.toInt).distinct
if (num.length == tam && num.length == accion.length)
Some(num.toArray)
else
None
}
@lnds
lnds / validar.fs
Created January 10, 2016 16:57
Validar en F#
let validar tam accion =
let cars = [for c in accion -> c]
if List.exists (fun c -> not (Char.IsDigit c)) cars then List.empty<int>
else
let org = List.map (fun c -> (int c) - (int '0')) cars
let num = List.distinct org
if (List.length org <> List.length num) || (List.length num <> tam) then List.empty<int>
else num
@lnds
lnds / ifs.kt
Created January 10, 2016 18:21
Ifs en Kotlin
if (!it.value.isDigit())
return null
if (it.index >= tam)
return null
else {
val digit = it.value.toInt() - '0'.toInt()
if (digit in num) return null
num = num.plus(digit)
}
@lnds
lnds / validar.hs
Created January 11, 2016 20:08
selección en Haskell
validar n xs
| length num /= n = []
| otherwise = num
where num = if not (all isDigit xs) then [] else remover_dups xs
@lnds
lnds / validar.clj
Created January 11, 2016 20:27
Validar en clojure
(defn validar [tam num]
(if (and (solo-digitos num) (largo-esperado tam num))
(map #(Character/digit % 10) (seq num))
nil))
@lnds
lnds / validar2.clj
Created January 11, 2016 20:34
Validar expandido
(defn validar [tam num]
(if (and (every? #(Character/isDigit %) num))
(let [cuenta-unicos (count (distinct num))]
(and
(= tam cuenta-unicos)
(= (count num) cuenta-unicos)))))
(map #(Character/digit % 10) (seq num))
nil))
@lnds
lnds / validar.erl
Created January 11, 2016 20:50
Validando en Erlang
Digitos = lists:all(fun (X) -> is_digit(X) end, Entrada),
if Digitos =:= true -> unicos(to_num(Entrada), Tam, Len) ;
Digitos =:= false -> [] end.