小编Tha*_*you的帖子

有没有Ruby-Way™来处理这种循环?

我正在尝试在平面阵列中为分组元素打印节标题.我只想让章节标题每组出现一次.

下面的例子可行,但对Ruby来说感觉相当不优雅.我确信必须有更好的方法来做到这一点;)

#!/usr/bin/ruby
foo = [1,1,1,1,2,2,2,3,3]

i = 0;

f = foo[i]
comp = f

while(i < foo.count) do
  puts "Section #{f}";

  while(f == comp) do
    puts f
    i += 1
    f = foo[i]
  end

  comp = f
end
Run Code Online (Sandbox Code Playgroud)

期望的输出

Section 1
1
1
1
1
Section 2
2
2
2
Section 3
3
3
Run Code Online (Sandbox Code Playgroud)

我希望有某种Array#currentArray#next实例方法,但看起来Ruby Array对象不保留内部迭代器.

ruby loops design-patterns

1
推荐指数
1
解决办法
172
查看次数

如何在PHP中对多维数组进行排序并保留键?

我有两个数组,我需要对A-> Z进行排序,但需要保留所有数字索引.我不知道如何处理这个问题.

注意:在$complex数组中,只要保留关联的键并对子数组内容进行排序,子数组的顺序就无关紧要了.

两个示例中都必须保留所有键.

<?php

$simple = array(
    20 => 'Hello',
    10 => 'Cat',
    30 => 'Dog'
);


$complex = array(
    30 => array(
        5 => 'foo',
        10 => 'bar'
        ),
    10 => array(
        4 => 'a',
        1 => 'b'
        ),
    20 => array()
);
Run Code Online (Sandbox Code Playgroud)

期望的输出

// simple
array(
    10 => Cat,
    30 => Dog,
    20 => Hello
)

// complex; order of top-level indexes (30, 10, and 20) is not important but the key *must* …
Run Code Online (Sandbox Code Playgroud)

php sorting multidimensional-array

1
推荐指数
1
解决办法
2579
查看次数

如何在JavaScript中对浮点数组进行排序?

我试过下面的例子,但现在正在使用正确的信息.

var fruits = [110.111, 1245.22222, 2.458, 0.001];
fruits.sort();
document.write(fruits);
Run Code Online (Sandbox Code Playgroud)

结果:

0.001,110.111,1245.22222,2.458
Run Code Online (Sandbox Code Playgroud)

但是我想要这样的东西

0.001,2.458,110..111,1245.22222
Run Code Online (Sandbox Code Playgroud)

这段代码有什么问题?

javascript sorting

1
推荐指数
1
解决办法
3412
查看次数

如何使用JavaScript在客户端之间共享数据?

我有一个JSON的变量,但我想让它在PHP页面上的每个人都能看到相同的变量内容.

这段代码应该解释我的更多情况.(我试图让它在页面不会重新加载的地方)

    var chats = [];
    var j = jQuery.noConflict();
    j(document).ready(function()
    {
        setInterval(function(i){
            var txt = "";
            var x;
            for (x in chats) {
              txt += chats[x] + " <br />";
            }
            document.getElementById("json").innerHTML = JSON.stringify(chats);
        }, 1000)
    });
    j(document).ready(function() {
        j('#post_button').click(function() {
            $text = $('[name=message]').val();
            $sender = $('#texta').val();
            chatstuff = {
                "sender" : $sender,
                "message" : $text,
            };
            chats.push(chatstuff);
            $('[name=message]').val("");
        });
    });
Run Code Online (Sandbox Code Playgroud)

所以当它出现时document.getElementById("json").innerHTML = JSON.stringify(chats);,我希望每个人都能够在同一页面上看到相同的聊天内容.

javascript php jquery

1
推荐指数
1
解决办法
612
查看次数

如何在递归函数中使用Control.Monad.Cont?

我正在提供这个问题的答案,我想到了使用Contmonad 的想法.我不知道Haskell足以解释为什么这个程序不起作用

import Control.Monad.Cont

fib1 n = runCont (slow n) id
  where
    slow 0 = return 0
    slow 1 = return 1
    slow n = do
      a <- slow (n - 1)
      b <- slow (n - 2)
      return a + b

main = do
  putStrLn $ show $ fib1 10
Run Code Online (Sandbox Code Playgroud)

错误 -

main.hs:10:18: error:
    • Occurs check: cannot construct the infinite type: a2 ~ m a2
    • In the second argument of ‘(+)’, namely ‘b’ …
Run Code Online (Sandbox Code Playgroud)

monads continuations haskell type-mismatch do-notation

1
推荐指数
1
解决办法
97
查看次数

如何在不使用regexp的情况下从JavaScript中删除字符串中的HTML标记?

我是编程新手,我正在解决这个问题.我已尝试使用string.slice()进行3次循环,但由于某种原因,它会打印一个空字符串.

你能否解释我的代码中发生了什么?为什么它打印错误的输出以及我如何纠正,而不是给我你正确答案的版本,以便我可以从错误中吸取教训.

测试输入是

<p><strong><em>PHP Exercises</em></strong></p>
Run Code Online (Sandbox Code Playgroud)

和输出应该是PHP练习

ps这不是一个PHP练习,我不会困惑

这是我的代码:

function remove(answer){ 

    var sen = answer.split("");
    var arr = [];
    for (var i = 0; i<answer.length; i++){
        if (answer[i] == "<"){
            for (var j = i; j<answer.length; j++){
                if (answer[j] == ">"){
                    for (var k = j; k<answer.length; k++){
                        if (answer[k] == "<"){
                            return answer.slice(j+1, k);                
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript

0
推荐指数
1
解决办法
490
查看次数

为什么'fn'无法访问'a'属性

var red = {a: 2};

red.fn = function(b){
    console.log("A: "+a ", B : "+b);
}

red.fn(20);
Run Code Online (Sandbox Code Playgroud)

这给出了错误:

a 没有定义

但我怀疑a已经可以作为函数的全局对象,fn那么为什么它不可访问.任何解释都会有所帮助.

javascript

0
推荐指数
1
解决办法
66
查看次数

如何在存储过程中使用事务?

我有一个 SQL 脚本,需要将其转换为参数化存储过程。我过去只编写过简单的函数,从未编写过带参数的复杂事务查询。

非常感谢任何帮助 - 下面简化了查询。该脚本实际上可以是包含交易和一些用户输入的任何内容。

-- transaction ensures i can clean up a mess, if one happens
begin;

-- parameters for the script; currently set manually before execution
set @parent_id := 123;
set @identifier := 'someid';

-- insert some row with user-specified values
insert into users (field1, field2) values (@parent_id, @identifier);

-- get the new id
set @user_id := last_insert_id();

-- do another insert
insert into usersmeta (user_id, field1, field2) values (@user_id, 1, 2);

-- if no errors happened …
Run Code Online (Sandbox Code Playgroud)

mysql sql stored-procedures

0
推荐指数
1
解决办法
6192
查看次数

访问箭头函数内的yield

由于无法创建箭头生成器函数, 因此从不在箭头函数的上下文中使用yield.

var arrowGenerator = *() => { };
Run Code Online (Sandbox Code Playgroud)

那么你应该能够在生成器函数的上下文中使用yield.就像这样

function* generator() {
    funcWithCallback((value) => {
       yield value;
    });
}
Run Code Online (Sandbox Code Playgroud)

但巴贝尔它使用产量箭头功能的情况下,而不是genreators.

我想这样做,所以你不需要返回一个带有值的回调函数,只是为了产生它.

function* gen() {
    yield function (callback) {
        funcWithCallback(callback);
    } 
}
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-6 babeljs

-3
推荐指数
1
解决办法
594
查看次数