I'm trying to read text from a file in SML. Eventually, I want a list of individual words; however, I'm struggling at how to convert between a TextIO.elem to a string. For example, if I write the following code it returns a TextIO.elem but I don't know how to convert it to a string so that I can concat it with another string
TextIO.input1 inStream
TextIO.elem is just a synonym for char, so you can use the str function to convert it to a string. But as I replied to elsewhere, I suggest using TextIO.inputAll to get a string right away.
Here is a function that takes an instream and delivers all (remaining) words in it:
val words = String.tokens Char.isSpace o TextIO.inputAll
The type of this function is TextIO.instream -> string list.
Related
So I would like to make a function in Rust where I can input a string and get the ciphertext out of it. I'm using the Kyber1024 KEM and I can't find a way to input a custom string to turn into a cipher.
Documentation: https://docs.rs/pqcrypto-kyber/0.7.6/pqcrypto_kyber/kyber1024/index.html
Crate: https://crates.io/crates/pqcrypto-kyber
The usage in the documentation just says this:
use pqcrypto_kyber::kyber1024::*;
let (pk, sk) = keypair();
let (ss1, ct) = encapsulate(&pk);
let ss2 = decapsulate(&ct, &sk);
assert!(ss1 == ss2);
Nowhere does it illustrate (as far as I can see) a way for a user to insert a custom string for example so it can get converted into ciphertext.
How do I do this?
The http request header has a 4k length limit.
I want to split the string which I want to include in the header based on this limit.
Should I use []byte(str) to split first then convert back to string using string([]byte) for each split part?
Is there any simpler way to do it?
In Go, a string is really just a sequence of bytes, and indexing a string produces bytes. So you could simply split your string into substrings by slicing it into 4kB substrings.
However, since UTF-8 characters can span multiple bytes, there is the chance that you will split in the middle of a character sequence. This isn't a problem if the split strings will always be joined together again in the same order at the other end before decoding, but if you try to decode each individually, you might end up with invalid leading or trailing byte sequences. If you want to guard against this, you could use the unicode/utf8 package to check that you are splitting on a valid leading byte, like this:
package httputil
import "unicode/utf8"
const maxLen = 4096
func SplitHeader(longString string) []string {
splits := []string{}
var l, r int
for l, r = 0, maxLen; r < len(longString); l, r = r, r+maxLen {
for !utf8.RuneStart(longString[r]) {
r--
}
splits = append(splits, longString[l:r])
}
splits = append(splits, longString[l:])
return splits
}
Slicing the string directly is more efficient than converting to []byte and back because, since a string is immutable and a []byte isn't, the data must be copied to new memory upon conversion, taking O(n) time (both ways!), whereas slicing a string simply returns a new string header backed by the same array as the original (taking constant time).
Hi newbie here and I am trying to master recursive functions in Erlang. This function looks like it should work, but I cannot understand why it does not. I am trying to create a function that will take N and a string and will print out to stdout the string the number of times.
My code:
-module(print_out_n_times).
-export([print_it/2).
print_it(0, _) ->
"";
print_it(N, string) ->
io:fwrite(string),
print_it(N - 1, string).
The error I get is:
** exception error: no function clause matching print_it(5, "hello')
How can I make this work ?
Variables in Erlang start with a capital letter. string is an atom, not a variable named "string". When you define a function print_it(N, string), it can be called with any value for the first argument and only the atom string as the second. Your code should work if you replace string with String:
print_it(N, String) ->
io:fwrite(String),
print_it(N - 1, String).
I am trying to convert the following variable:
- final "in1.txt";
val it = [|[#"S",#".",#".",#"."],[#".",#".",#".",#"."],[#"W",#".",#"X",#"W"],
[#".",#".",#"X",#"E"]|] : char list array
from 'char list array' to 'char array array' in SMLNJ. The only reason I want to do this is because I need to be able to randomly iterate through this data, to perform a Dijkstra-like algorithm for a school project (if there 's a more efficient way to make this data iteratable, I am all ears). Is there a way to do this? The function that reads the input file and returns the above is this (I found it in Stack Overflow):
fun linelist file =
let
open Char
open String
open List
val instr = TextIO.openIn file
val str = TextIO.inputAll instr
in
tokens isSpace str
before
TextIO.closeIn instr
end
fun final file =
let
fun getsudo file = map explode (linelist file)
in
Array.fromList (getsudo file)
end
and the input files that need to be processed are like the one that follows:
S...
....
W.XW
..XE
You might want to try a different way to read this (space delivery) map (to help Lakis -- yes I am a classmate of yours).
fun parse file =
let
fun next_String input = (TextIO.inputAll input)
val stream = TextIO.openIn file
val a = next_String stream
val lista = explode(a)
in
lista
end
Parse is a function that gets all the contents from a text file and saves them in string a. Then, the function explode (function of String Signature of the SML NJ) creates a list, called lista. The elements of the list are the characters of the string a in the same order.
Then, you can create another function that saves the contents of the list to an array. Each row of the array will contain the characters of the list until #"\n" comes up.
I'm trying to replace some characters in a String From a map
Case 1
map= ['O':'0', 'L':'1', 'Z':'2', 'E':'3']
"Hey".toUpperCase().toCharArray().each{
print map.get(it,it)
}
The result is
HEY
Case 2 : I dont use toCharArray()
"Hey".toUpperCase().each{
print map.get(it,it)
}
The result is like expected
H3Y
So I tried several alternatives when using toCharArray(), and the only way to access the value is to use map."$it"
Why i can only use map."$it" to access my map when using toCharArray() ?
Because you are trying to get a value from a map using a char whilst every key there are String, and they are not equals:
assert !'E'.equals('E' as char)
$it works because it is converted to String:
e = 'E' as char
assert "$e".toString().equals('E')
(Note the toString() is needed, otherwise the comparison will happen between String and GStringImpl which are not equals)