f#

Built in f# operator to compose functions with the same input but different outputs?

心不动则不痛 提交于 2020-01-04 11:09:29
问题 I understand the << compose operator takes two functions that both take in and return the same type. e.g. (lhs:'a -> 'a) -> (rhs:'a -> 'a) -> 'a I often find myself wanting something like (lhs:'a -> 'b) -> (rhs:'c -> 'b) -> 'b in cases where I'm interested in side affects and not the return value 'b is probably the unit type. This is only when I have two lines in succession where I'm persisting something to a database. Is there a built in function or idiomatic F# way of doing this without

How to close a stream properly?

自作多情 提交于 2020-01-04 10:59:08
问题 I have been assigned a task to write a program that will: Open a file. Read the content. Replace a specific word with another word. Save the changes to the file. I know for sure that my code can open, read and replace words. The problem occurs when i add the "Save the changes to the file" - part. Here is the code: open System.IO //Getting the filename, needle and replace-word. System.Console.WriteLine "What is the name of the file?" let filename = string (System.Console.ReadLine ()) System

What is the most idiomatic way of representing errors in F#

随声附和 提交于 2020-01-04 10:23:10
问题 I'm working on F# project and I wonder what is the best practice to return domain error using Result type in F#. There are several ways of doing it which I consider: Inherited exceptions type DomainException(message) = inherit Exception(message) type ItemNotFoundException(item) = inherit DomainException(sprintf "Item %s is not found" item) let findItem item = match item with | Some x -> Ok x | None -> Error(new ItemNotFoundException("someitem")) Custom record type type DomainError = { Name :

What is the most idiomatic way of representing errors in F#

白昼怎懂夜的黑 提交于 2020-01-04 10:23:01
问题 I'm working on F# project and I wonder what is the best practice to return domain error using Result type in F#. There are several ways of doing it which I consider: Inherited exceptions type DomainException(message) = inherit Exception(message) type ItemNotFoundException(item) = inherit DomainException(sprintf "Item %s is not found" item) let findItem item = match item with | Some x -> Ok x | None -> Error(new ItemNotFoundException("someitem")) Custom record type type DomainError = { Name :

What is the most idiomatic way of representing errors in F#

微笑、不失礼 提交于 2020-01-04 10:22:22
问题 I'm working on F# project and I wonder what is the best practice to return domain error using Result type in F#. There are several ways of doing it which I consider: Inherited exceptions type DomainException(message) = inherit Exception(message) type ItemNotFoundException(item) = inherit DomainException(sprintf "Item %s is not found" item) let findItem item = match item with | Some x -> Ok x | None -> Error(new ItemNotFoundException("someitem")) Custom record type type DomainError = { Name :

Why does my StreamReader.ReadLine () in F# not read \n as a new line?

丶灬走出姿态 提交于 2020-01-04 05:26:37
问题 I am currently trying to read a file as this: 53**7****\n6**195***\n*98****6*\n8***6***3\n4**8*3**1\n7***2***6\n*6****28*\n***419**5\n****8**79\n And write it into the screen, but with new lines instead of the /n. On the msdn description of the method StreamReader.ReadLine () it says that: A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n"). The string that is returned does

F# FSI, Change working directory

女生的网名这么多〃 提交于 2020-01-04 05:12:09
问题 I have the following file located in a "New Folder" of Desktop: // File location: "C:\Users\my_user_name\Desktop\New folder\AddOne.fs" // module internal AddOneModule let AddOneFunction x = x + 1 I can access this file by using #load on the full path name using FSI F# Interactive. Microsoft (R) F# Interactive version 4.1 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > #load "C:\Users\my_user_name\Desktop\New folder\AddOne.fs";; //[Loading C:\Users\my_user

Remove a single non-unique value from a sequence in F#

巧了我就是萌 提交于 2020-01-04 04:04:07
问题 I have a sequence of integers representing dice in F#. In the game in question, the player has a pool of dice and can choose to play one (governed by certain rules) and keep the rest. If, for example, a player rolls a 6, 6 and a 4 and decides to play one the sixes, is there a simple way to return a sequence with only one 6 removed? Seq.filter (fun x -> x != 6) dice removes all of the sixes, not just one. 回答1: the below code will work for a list (so not any seq but it sounds like the sequence

Facebook Oauth 2.0 and rate limiting

心不动则不痛 提交于 2020-01-04 02:05:29
问题 I am trying to extract Facebook ids from a list of emails using the url: let url = "https://graph.facebook.com/search?q=" + email.Replace("@","%40") + "&type=user&access_token=" + facebook.Token After around 600 id extracts I get the error "The remote server returned an error: (400) Bad Request.". Is the facebook API/Search rate limited? If so where is the doc? 回答1: Facebook limits you to 600 calls/600 sec - but it does reset after 10 minutes. I can't find the documentation at the moment, but

Multiplying a string in F#

旧时模样 提交于 2020-01-04 01:58:13
问题 I have a question I am rather unsure about. My questions is as follows let myFunc (text:string) (times:int) = .... What I want this function to do is put the string together as many times as specified by the times parameter. if input = "check " 3 I want the output string = "check check check" I have tried with a loop, but couldn't seem to make it work. Anyone? 回答1: Actually the function is already in String module: let multiply text times = String.replicate times text To write your own