Last active
September 18, 2015 08:34
-
-
Save mastoj/a549704b0ab581fbf3b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
let digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";; | |
let ex = "101010101011101010101011101010101011101010101011";; | |
let convert (chars:string) (ul) = | |
let baseLength = (int64 (chars.Length)) | |
let rec convert' (acc,rest) = | |
if rest = 0L then acc |> List.rev | |
else convert' (chars.[int (rest % baseLength)]::acc,(rest/baseLength)) | |
convert' ([],ul) | |
let decimalToBinary (dec:Int64) = Convert.ToString(dec, 2) | |
let encodedToDecimal (digits:string) (c:char) = (int64 (digits.IndexOf(c))) | |
let sumCodedValue (digits:string) decimals = | |
let multiplier = (float digits.Length) | |
decimals | |
|> Seq.mapi (fun i v -> (int64 ((float v)*(multiplier**(float i))))) | |
|> Seq.sum | |
let stringToDec i = Convert.ToInt64(i, 2) | |
let listToString chars = chars |> Seq.map string |> String.concat "" | |
let stringToList (str:string) = str.ToCharArray() |> Array.toList | |
let encode = stringToDec >> convert digits >> listToString | |
let decode = Seq.map (encodedToDecimal digits) >> sumCodedValue digits >> decimalToBinary | |
let wat = ex |> encode | |
let actual = wat |> decode | |
actual = ex | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open System.Text.RegularExpressions | |
let matches pattern str = Regex.Matches(str, pattern) | |
let chunk = matches ".{1,6}" >> Seq.cast >> Seq.map (fun (m:Match) -> m.Groups.[0].Captures.[0].ToString()) | |
let encodeChunk bits = string digits.[Convert.ToInt32(bits, 2)] | |
let encode2 = chunk >> Seq.map encodeChunk >> String.concat "" | |
let indexOfChar (c:char) = digits.IndexOf(c) | |
let toBinary (i:int) = Convert.ToString(i, 2).PadLeft(6, '0') | |
let charArr (str:string) = str.ToCharArray() | |
let decode2 = charArr >> Seq.map indexOfChar >> Seq.map toBinary >> Seq.concat >> String.Concat | |
let a = "101110" | |
a |> encode2 |> decode2 = a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment