如何将此代码和我的想法转换为功能性思维模式(Clojure)? [英] How do convert this code and my thinking to a functional mindset (Clojure)?

查看:196
本文介绍了如何将此代码和我的想法转换为功能性思维模式(Clojure)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何将这段JavaScript代码转换为Clojure?



我试图绘制一个(x,y)世界,其中单元格根据填充属性。在下面的示例中,我试图打印行和列,但下一步是移动填充属性(上,下,左,右)。所以,如果我不打印数据结构,我不希望得到一个答案。



我的目标是了解如何考虑这个问题。一种功能方式。我很容易用JavaScript中的可变状态来解决这个问题。在解决问题的过程中,我能够轻松地推断如何使用JavaScript编写代码,但是当我在Clojure中做同样的工作时,我不知所措。所以,我觉得这个问题的一个很好的答案就是帮助我理解如何以功能的方式来思考这种问题。



我看过很多会谈并阅读了一堆关于Clojure和函数式编程的文章,但是当编写代码时,我很难开始。

  var world = [
[{x:1,y:4,fill:0},{x:2,y:4,fill :0},{x:3,y:4,fill:0},{x:4,y:4,fill:0}],
[{x:1,y:3,fill:0 },{x:2,y:3,fill:0},{x:3,y:3,fill:0},{x:4,y:3,fill:0}],
[ {x:1,y:2,fill:0},{x:2,y:2,fill:0},{x:3,y:2,fill:0},{x:4,y:2 ,填充:0}],
[{x:1,y:1,fill:0},{x:2,y:1,fill:0},{x:3,y:1,fill :1},{x:4,y:1,fill:0}]
];

函数printworld(world){
var out =; (var var = 0; row< world.length; row ++){
for(var col = 0; col< world [row] .length; col ++){
out + = [ +世界[行] [山口] + .fill伪 ];
}
out + =\\\

}
console.log(out);
}

printworld(world);

输出如下:

  [0] [0] [0] [0] 
[0] [0] [0] [0]
[0] [0] [0] [0]
[0] [0] [1] [0]



编辑:花了一些时间在4clojure.com上解决问题之后,我意识到我正试图解决一个更大的问题(例如Clojure中的国际象棋游戏),而不是我准备解决的问题。在4clojure.com上创建最基本的功能一直很困难,但它正在建立一个稳定的工作知识,以便如何使用功能方法来解决问题。

解决方案如何以功能性的方式开始思考?没有捷径和快速答案。你必须投入大量时间试图以功能方式进行编码。


您可以通过变换简单算法开始。但重要的是要记住,在函数式编程中,您关心的是数据流,而不是如何指示CPU执行此操作。

了解您的语言的核心功能非常重要。从这些核心功能中,您可以开始转换数据。如果你愿意的话,可以使用乐高或DNA条纹。

如果你对Clojure特别感兴趣,那么我建议你花点时间在4Clojure和

永远记住:默认情况下,OOP不容易,FP默认情况下并不容易。


How do I convert this JavaScript code to Clojure?

I am trying to draw a (x,y) world where the cells are on or off according to the fill property. In the example below I am trying to print the rows then columns but my next step is to move the fill property around (up, down, left, right). So, I don't want an answer which wouldn't work if I were not printing the data structure.

My goal is to understand how to think about this problem in a functional way. It was easy for me to solve this problem with mutable state in JavaScript. While working on the solution, I was able to easily reason about how to write the code in JavaScript but when I came to do the same in Clojure, I was at a loss. So, I feel like a good answer to this question would be to help me understand how to think about this kind of problem in a functional way.

I have watched many talks and read a bunch of articles about Clojure and functional programming but when it becomes to writing the code, it's difficult for me to get started.

var world = [
    [{ x: 1, y: 4, fill:0 }, { x: 2, y: 4, fill:0 }, { x: 3, y: 4, fill:0 }, { x: 4, y: 4, fill:0 }],
    [{ x: 1, y: 3, fill:0 }, { x: 2, y: 3, fill:0 }, { x: 3, y: 3, fill:0 }, { x: 4, y: 3, fill:0 }],
    [{ x: 1, y: 2, fill:0 }, { x: 2, y: 2, fill:0 }, { x: 3, y: 2, fill:0 }, { x: 4, y: 2, fill:0 }],
    [{ x: 1, y: 1, fill:0 }, { x: 2, y: 1, fill:0 }, { x: 3, y: 1, fill:1 }, { x: 4, y: 1, fill:0 }]
];

function printworld(world) {
    var out = "";
    for(var row=0; row<world.length; row++) {
        for(var col=0; col<world[row].length; col++) {
            out += "["+world[row][col].fill+"]";
        }
        out += "\n"
    }
    console.log(out);
}

printworld(world);

The output looks like:

[0][0][0][0]
[0][0][0][0]
[0][0][0][0]
[0][0][1][0]

EDIT: After spending time working through the problems at 4clojure.com, I realized that I was trying to bite off a larger problem (ex. a Chess game in Clojure) than I was ready to solve. Creating the most basic functions at 4clojure.com has been difficult but it's building a steady working knowledge on how to approach a solution using the functional method.

解决方案

How to start thinking in a functional way? There is no short cuts and quick answers. You have to invest a good amount of time trying to code in functional way.

You can start by morphing simple algorithms. But it is important to keep in mind that in functional programming you care about data flow, not how to instruct the CPU to do this and that.

It is important to know the core functions in your language. From those core functions, you can start transform you data. Kind of Lego or DNA stripes if you want.

If you are interested in Clojure particularly, then I recommend to spend good time on 4Clojure and Land of Lisp.

And Clojure IRC is a wonderful place to learn from Rockstars Clojure developers. Friendly community and helpful for sure.

Always remember: "OOP isn't easy by default and FP isn't hard by default".

这篇关于如何将此代码和我的想法转换为功能性思维模式(Clojure)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆