我为一家技术公司工作,该公司比产品发货做更多的原型设计.我刚才被问到C#和F#有什么区别,为什么MS创建F#以及它会比C#更好.
我一直在使用这种语言,我喜欢它,所以我可以很容易地继续讨论F#的强大功能,但是我缺乏C#的经验来说明为什么我们应该使用它而不是另一种.
使用C#vs F#或F#vs C#有什么好处?
比如说,您只是查询了一个数据库,并收到了这个2D数组.
$results = array(
array('id' => 1, 'name' => 'red' , 'spin' => 1),
array('id' => 2, 'name' => 'green', 'spin' => -1),
array('id' => 3, 'name' => 'blue' , 'spin' => .5)
);
Run Code Online (Sandbox Code Playgroud)
我经常发现自己写这样的循环.
foreach($results as $result)
$names[] = $result['name'];
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有办法在不使用循环的情况下获取此数组$名称?使用回调函数计为使用循环.
以下是获取每个字段的更一般示例.
foreach($results as $result)
foreach($result as $key => $value)
$fields[$key][] = $value;
Run Code Online (Sandbox Code Playgroud) 我试图找出使用管道运算符|>进入对象创建的正确语法.目前我正在使用静态成员来创建对象,并且只是为了管道.这是简化版.
type Shape =
val points : Vector[]
new (points) =
{ points = points; }
static member create(points) =
Shape(points)
static member concat(shapes : Shape list) =
shapes
|> List.map (fun shape -> shape.points)
|> Array.concat
|> Shape.create
Run Code Online (Sandbox Code Playgroud)
我想做的事 ...
static member concat(shapes : Shape list) =
shapes
|> List.map (fun shape -> shape.points)
|> Array.concat
|> (new Shape)
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?我不想通过使用静态成员create重复我的构造函数来复制代码.
更新 构造函数是F#4.0的一流函数
在F#4.0中,正确的语法是.
static member concat(shapes : Shape list) =
shapes
|> List.map (fun shape -> shape.points)
|> Array.concat …Run Code Online (Sandbox Code Playgroud) 是否有内置版本的类型转换功能可以保留单位,如果没有,我将如何制作它们?因此,例如使用此代码,如何将intWithSecondsMeasure转换为浮点数而不会丢失度量或乘以1.0<s>?
[<Measure>] type s
let intWithSecondsMeasure = 1<s>
let justAFloat = float intWithSecondsMeasure
Run Code Online (Sandbox Code Playgroud) 我一直在研究C++ 11中的新功能,看起来它可以使用它以非常实用的编程风格进行编程.我已经习惯在F#中使用List,Seq,Array类型,我看不出为什么他们的成员无法移植到某种C++ 11模板中.对于混合函数式编程风格,您在使用C++ 11与F#之类的东西中看到了哪些问题或优势?functional一旦C++ 11出现,也许Boost的家伙会成为新人.
我正在尝试使用Seq.cache和我所做的函数,它返回一个数字序列,直到数字N,不包括数字1.我无法弄清楚如何将缓存序列保留在范围内但仍然使用它在我的定义中.
let rec primesNot1 n =
{2 .. n}
|> Seq.filter (fun i ->
(primesNot1 (i / 2) |> Seq.for_all (fun o -> i % o <> 0)))
|> Seq.append {2 .. 2}
|> Seq.cache
Run Code Online (Sandbox Code Playgroud)
有关如何使用Seq.cache来加快速度的任何想法?目前它不断从范围中退出,只会降低性能.
我正在尝试学习当前接受的c ++ 11功能,而我遇到了auto和decltype的问题.作为一个学习练习,我正在使用一些通用函数扩展std类列表.
template<class _Ty, class _Ax = allocator<_Ty>>
class FList : public std::list<_Ty, _Ax>
{
public:
void iter(const function<void (_Ty)>& f)
{
for_each(begin(), end(), f);
}
auto map(const function<float (_Ty)>& f) -> FList<float>*
{
auto temp = new FList<float>();
for (auto i = begin(); i != end(); i++)
temp->push_back(f(*i));
return temp;
}
};
auto *ints = new FList<int>();
ints->push_back(2);
ints->iter([](int i) { cout << i; });
auto *floats = ints->map([](int i) { return (float)i; });
floats->iter([](float i) { cout …Run Code Online (Sandbox Code Playgroud) 我一直在为一些F#模块添加一些方便的方法,比如List.
type Microsoft.FSharp.Collections.FSharpList<'a> with //'
static member iterWhile (f:'a -> bool) (ls:'a list) =
let rec iterLoop f ls =
match ls with
| head :: tail -> if f head then iterLoop f tail
| _ -> ()
iterLoop f ls
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以添加突变?我知道List是不可变的,那么如何向Ref类型List添加一个可变方法.像这样的东西.
type Ref<'a when 'a :> Microsoft.FSharp.Collections.FSharpList<'a> > with //'
member this.AppendMutate element =
this := element :: !this
Run Code Online (Sandbox Code Playgroud)
或者是否有某种方法来约束泛型只接受一个可变的?
我不确定这个的语法.我正在尝试将这个C#代码翻译成F#.
struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
public class IdleTimer
{
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
}
Run Code Online (Sandbox Code Playgroud)
这就是我到目前为止所拥有的.
type LASTINPUTINFO = {
cbSize : UInt32;
dwTime : UInt32;
}
type IdleTimer =
[<DllImport("User32.dll")>]
[<return: MarshalAs(UnmanagedType.Bool)>]
extern GetLastInputInfo(plii : LASTINPUTINFO ref)
Run Code Online (Sandbox Code Playgroud) 我在制作序列时遇到了一些麻烦.基本上我需要将序列切割成一系列数组.Seq.windowed几乎做到了,但我不想要重复的元素.
我可以通过首先将所有内容读入数组来获得我想要的内容,但我宁愿使用序列.
let array_chunk s (a:int[]) =
Array.init (a.Length / s) (fun i -> Array.sub a (i * s) s)
someSequence |> Seq.to_array |> array_chunk 5
Run Code Online (Sandbox Code Playgroud)