你如何将javascript reduce()翻译成Swift 2?

时间:2015-11-30 18:20:39

标签: javascript functional-programming swift2 reduce

...提供js reduce()采用如下的3参数函数?

var relevance = _(s.words).reduce( function (memo, freq, w) {
    var local = Math.log( 1 + freq );
    var global = Math.log( N / containing(w).length );
    return memo = memo + (local * global);
}, 0);

到目前为止,我来到了:

let relevance = s.words.reduce(0) {
   (memo, freq, w) in

   var local = log( 1 + freq )
   var global = log( N / containing(w).length )

   return memo = memo + (local * global)
} // ~reduce()

但是,由于arg不匹配的数量,reduce()无法编译。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用reduce功能,如下所示:

// indexValue is of type (index: Int, element: Int)
[1, 3, 5].enumerate().reduce(0) { previousValue, indexValue in
    // access index
    indexValue.index
    // access value
    indexValue.element
    return previousValue + indexValue.element
}

// returns also 9

在您的情况下,您可以通过调用enumerate:

来获取值的索引
let relevance = s.words.enumerate().reduce(0) {
   (memo, freqW) in

   let freq = Double(freqW.index)
   let w = freqW.element

   // use let if you don't modify the value
   let local = log( 1 + freq )
   // you probably need to convert some of the variables to Double
   let global = log( N / containing(w).length )

   // in the next iteration memo will be this returned value
   return memo + (local * global)
}

您的代码:

<h:form id="form">      
             <p:commandButton value="Salvar" icon="ui-icon-document" class="botaoDataTable"
                actionListener="#{cenarioBean.cadastrarAvaliacaoDeArea}" process="@this"/>

             <h:outputText style="margin-left:2%;" id="probabilidadeSucesso" value="Probabilidade de Sucesso: #{controleBean.calcularProbabilidadeDeSucesso()}" />

             <p:panelGrid style="float:left; margin-left:2%; margin-top:1%; width:70%;" styleClass="semBorda">  
                <p:row>
                    <p:column><h:outputText style="font-size:90%;" value="Distância entre o local de perfuração e o poço produtor mais próximo" /></p:column>
                    <p:column style="width:30% !important;">                    
                        <p:selectOneMenu id="distanciaAv" value="#{cenarioBean.avaliacaoArea.distanciaPocoProdutor}" class="componentePF text">
                            <f:selectItem itemLabel="Escolha uma Opção" itemDisabled="true" noSelectionOption="true" /> 
                            <f:selectItem itemLabel="Menor ou Igual a 2,5 Km" itemValue="A" /> 
                            <f:selectItem itemLabel="Até 10 Km" itemValue="B" />
                            <f:selectItem itemLabel="Entre 10 Km e 50 Km" itemValue="C" />
                            <f:selectItem itemLabel="Acima de 50 Km" itemValue="D" /> 
                        </p:selectOneMenu>
                    </p:column>
                </p:row>

                <p:row>
                    <p:column><h:outputText style="font-size:90%;" value="O local de perfuração pertence à mesma formação geológica do poço produtor" /></p:column>
                    <p:column style="width:30% !important;">                    
                        <p:selectOneMenu id="formGeo" value="#{cenarioBean.avaliacaoArea.mesmaFormacaoGeologica}" class="componentePF text">
                            <f:selectItem itemLabel="Escolha uma Opção" itemDisabled="true" noSelectionOption="true" /> 
                            <f:selectItem itemLabel="Sim" itemValue="S"  /> 
                            <f:selectItem itemLabel="Não" itemValue="N" /> 
                        </p:selectOneMenu>
                    </p:column>
                </p:row>

                <p:row>
                    <p:column><h:outputText style="font-size:90%;" value="Há evidências positivas para prospecção" /></p:column>
                    <p:column style="width:30% !important;">                    
                        <p:selectOneMenu id="prospecpos" value="#{cenarioBean.avaliacaoArea.evidenciasProspeccao}" class="componentePF text">
                            <f:selectItem itemLabel="Escolha uma Opção" itemDisabled="true" noSelectionOption="true" /> 
                            <f:selectItem itemLabel="Sim" itemValue="S" /> 
                            <f:selectItem itemLabel="Não" itemValue="N" /> 
                        </p:selectOneMenu>
                    </p:column>
                </p:row>
             </p:panelGrid>

         </h:form>